Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1 | /* |
| 2 | * S5P/EXYNOS4 SoC series camera host interface media device driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Samsung Electronics Co., Ltd. |
| 5 | * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published |
| 9 | * by the Free Software Foundation, either version 2 of the License, |
| 10 | * or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/bug.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/pm_runtime.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/version.h> |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame^] | 25 | #include <media/v4l2-ctrls.h> |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 26 | #include <media/media-device.h> |
| 27 | |
| 28 | #include "fimc-core.h" |
| 29 | #include "fimc-mdevice.h" |
| 30 | #include "mipi-csis.h" |
| 31 | |
| 32 | static int __fimc_md_set_camclk(struct fimc_md *fmd, |
| 33 | struct fimc_sensor_info *s_info, |
| 34 | bool on); |
| 35 | /** |
| 36 | * fimc_pipeline_prepare - update pipeline information with subdevice pointers |
| 37 | * @fimc: fimc device terminating the pipeline |
| 38 | * |
| 39 | * Caller holds the graph mutex. |
| 40 | */ |
| 41 | void fimc_pipeline_prepare(struct fimc_dev *fimc, struct media_entity *me) |
| 42 | { |
| 43 | struct media_entity_graph graph; |
| 44 | struct v4l2_subdev *sd; |
| 45 | |
| 46 | media_entity_graph_walk_start(&graph, me); |
| 47 | |
| 48 | while ((me = media_entity_graph_walk_next(&graph))) { |
| 49 | if (media_entity_type(me) != MEDIA_ENT_T_V4L2_SUBDEV) |
| 50 | continue; |
| 51 | sd = media_entity_to_v4l2_subdev(me); |
| 52 | |
| 53 | if (sd->grp_id == SENSOR_GROUP_ID) |
| 54 | fimc->pipeline.sensor = sd; |
| 55 | else if (sd->grp_id == CSIS_GROUP_ID) |
| 56 | fimc->pipeline.csis = sd; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * __subdev_set_power - change power state of a single subdev |
| 62 | * @sd: subdevice to change power state for |
| 63 | * @on: 1 to enable power or 0 to disable |
| 64 | * |
| 65 | * Return result of s_power subdev operation or -ENXIO if sd argument |
| 66 | * is NULL. Return 0 if the subdevice does not implement s_power. |
| 67 | */ |
| 68 | static int __subdev_set_power(struct v4l2_subdev *sd, int on) |
| 69 | { |
| 70 | int *use_count; |
| 71 | int ret; |
| 72 | |
| 73 | if (sd == NULL) |
| 74 | return -ENXIO; |
| 75 | |
| 76 | use_count = &sd->entity.use_count; |
| 77 | if (on && (*use_count)++ > 0) |
| 78 | return 0; |
| 79 | else if (!on && (*use_count == 0 || --(*use_count) > 0)) |
| 80 | return 0; |
| 81 | ret = v4l2_subdev_call(sd, core, s_power, on); |
| 82 | |
| 83 | return ret != -ENOIOCTLCMD ? ret : 0; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * fimc_pipeline_s_power - change power state of all pipeline subdevs |
| 88 | * @fimc: fimc device terminating the pipeline |
| 89 | * @state: 1 to enable power or 0 for power down |
| 90 | * |
| 91 | * Need to be called with the graph mutex held. |
| 92 | */ |
| 93 | int fimc_pipeline_s_power(struct fimc_dev *fimc, int state) |
| 94 | { |
| 95 | int ret = 0; |
| 96 | |
| 97 | if (fimc->pipeline.sensor == NULL) |
| 98 | return -ENXIO; |
| 99 | |
| 100 | if (state) { |
| 101 | ret = __subdev_set_power(fimc->pipeline.csis, 1); |
| 102 | if (ret && ret != -ENXIO) |
| 103 | return ret; |
| 104 | return __subdev_set_power(fimc->pipeline.sensor, 1); |
| 105 | } |
| 106 | |
| 107 | ret = __subdev_set_power(fimc->pipeline.sensor, 0); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | ret = __subdev_set_power(fimc->pipeline.csis, 0); |
| 111 | |
| 112 | return ret == -ENXIO ? 0 : ret; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * __fimc_pipeline_initialize - update the pipeline information, enable power |
| 117 | * of all pipeline subdevs and the sensor clock |
| 118 | * @me: media entity to start graph walk with |
| 119 | * @prep: true to acquire sensor (and csis) subdevs |
| 120 | * |
| 121 | * This function must be called with the graph mutex held. |
| 122 | */ |
| 123 | static int __fimc_pipeline_initialize(struct fimc_dev *fimc, |
| 124 | struct media_entity *me, bool prep) |
| 125 | { |
| 126 | int ret; |
| 127 | |
| 128 | if (prep) |
| 129 | fimc_pipeline_prepare(fimc, me); |
| 130 | if (fimc->pipeline.sensor == NULL) |
| 131 | return -EINVAL; |
| 132 | ret = fimc_md_set_camclk(fimc->pipeline.sensor, true); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | return fimc_pipeline_s_power(fimc, 1); |
| 136 | } |
| 137 | |
| 138 | int fimc_pipeline_initialize(struct fimc_dev *fimc, struct media_entity *me, |
| 139 | bool prep) |
| 140 | { |
| 141 | int ret; |
| 142 | |
| 143 | mutex_lock(&me->parent->graph_mutex); |
| 144 | ret = __fimc_pipeline_initialize(fimc, me, prep); |
| 145 | mutex_unlock(&me->parent->graph_mutex); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * __fimc_pipeline_shutdown - disable the sensor clock and pipeline power |
| 152 | * @fimc: fimc device terminating the pipeline |
| 153 | * |
| 154 | * Disable power of all subdevs in the pipeline and turn off the external |
| 155 | * sensor clock. |
| 156 | * Called with the graph mutex held. |
| 157 | */ |
| 158 | int __fimc_pipeline_shutdown(struct fimc_dev *fimc) |
| 159 | { |
| 160 | int ret = 0; |
| 161 | |
| 162 | if (fimc->pipeline.sensor) { |
| 163 | ret = fimc_pipeline_s_power(fimc, 0); |
| 164 | fimc_md_set_camclk(fimc->pipeline.sensor, false); |
| 165 | } |
| 166 | return ret == -ENXIO ? 0 : ret; |
| 167 | } |
| 168 | |
| 169 | int fimc_pipeline_shutdown(struct fimc_dev *fimc) |
| 170 | { |
| 171 | struct media_entity *me = &fimc->vid_cap.vfd->entity; |
| 172 | int ret; |
| 173 | |
| 174 | mutex_lock(&me->parent->graph_mutex); |
| 175 | ret = __fimc_pipeline_shutdown(fimc); |
| 176 | mutex_unlock(&me->parent->graph_mutex); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs |
| 183 | * @fimc: fimc device terminating the pipeline |
| 184 | * @on: passed as the s_stream call argument |
| 185 | */ |
| 186 | int fimc_pipeline_s_stream(struct fimc_dev *fimc, int on) |
| 187 | { |
| 188 | struct fimc_pipeline *p = &fimc->pipeline; |
| 189 | int ret = 0; |
| 190 | |
| 191 | if (p->sensor == NULL) |
| 192 | return -ENODEV; |
| 193 | |
| 194 | if ((on && p->csis) || !on) |
| 195 | ret = v4l2_subdev_call(on ? p->csis : p->sensor, |
| 196 | video, s_stream, on); |
| 197 | if (ret && ret != -ENOIOCTLCMD) |
| 198 | return ret; |
| 199 | if ((!on && p->csis) || on) |
| 200 | ret = v4l2_subdev_call(on ? p->sensor : p->csis, |
| 201 | video, s_stream, on); |
| 202 | return ret == -ENOIOCTLCMD ? 0 : ret; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Sensor subdevice helper functions |
| 207 | */ |
| 208 | static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd, |
| 209 | struct fimc_sensor_info *s_info) |
| 210 | { |
| 211 | struct i2c_adapter *adapter; |
| 212 | struct v4l2_subdev *sd = NULL; |
| 213 | |
| 214 | if (!s_info || !fmd) |
| 215 | return NULL; |
| 216 | |
| 217 | adapter = i2c_get_adapter(s_info->pdata->i2c_bus_num); |
| 218 | if (!adapter) |
| 219 | return NULL; |
| 220 | sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter, |
| 221 | s_info->pdata->board_info, NULL); |
| 222 | if (IS_ERR_OR_NULL(sd)) { |
| 223 | v4l2_err(&fmd->v4l2_dev, "Failed to acquire subdev\n"); |
| 224 | return NULL; |
| 225 | } |
| 226 | v4l2_set_subdev_hostdata(sd, s_info); |
| 227 | sd->grp_id = SENSOR_GROUP_ID; |
| 228 | |
| 229 | v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n", |
| 230 | s_info->pdata->board_info->type); |
| 231 | return sd; |
| 232 | } |
| 233 | |
| 234 | static void fimc_md_unregister_sensor(struct v4l2_subdev *sd) |
| 235 | { |
| 236 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 237 | |
| 238 | if (!client) |
| 239 | return; |
| 240 | v4l2_device_unregister_subdev(sd); |
| 241 | i2c_unregister_device(client); |
| 242 | i2c_put_adapter(client->adapter); |
| 243 | } |
| 244 | |
| 245 | static int fimc_md_register_sensor_entities(struct fimc_md *fmd) |
| 246 | { |
| 247 | struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data; |
| 248 | struct fimc_dev *fd = NULL; |
| 249 | int num_clients, ret, i; |
| 250 | |
| 251 | /* |
| 252 | * Runtime resume one of the FIMC entities to make sure |
| 253 | * the sclk_cam clocks are not globally disabled. |
| 254 | */ |
| 255 | for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++) |
| 256 | if (fmd->fimc[i]) |
| 257 | fd = fmd->fimc[i]; |
| 258 | if (!fd) |
| 259 | return -ENXIO; |
| 260 | ret = pm_runtime_get_sync(&fd->pdev->dev); |
| 261 | if (ret < 0) |
| 262 | return ret; |
| 263 | |
| 264 | WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor)); |
| 265 | num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor)); |
| 266 | |
| 267 | fmd->num_sensors = num_clients; |
| 268 | for (i = 0; i < num_clients; i++) { |
| 269 | fmd->sensor[i].pdata = &pdata->isp_info[i]; |
| 270 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true); |
| 271 | if (ret) |
| 272 | break; |
| 273 | fmd->sensor[i].subdev = |
| 274 | fimc_md_register_sensor(fmd, &fmd->sensor[i]); |
| 275 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false); |
| 276 | if (ret) |
| 277 | break; |
| 278 | } |
| 279 | pm_runtime_put(&fd->pdev->dev); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * MIPI CSIS and FIMC platform devices registration. |
| 285 | */ |
| 286 | static int fimc_register_callback(struct device *dev, void *p) |
| 287 | { |
| 288 | struct fimc_dev *fimc = dev_get_drvdata(dev); |
| 289 | struct fimc_md *fmd = p; |
| 290 | int ret; |
| 291 | |
| 292 | if (!fimc || !fimc->pdev) |
| 293 | return 0; |
| 294 | if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS) |
| 295 | return 0; |
| 296 | |
| 297 | fmd->fimc[fimc->pdev->id] = fimc; |
| 298 | ret = fimc_register_m2m_device(fimc, &fmd->v4l2_dev); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | ret = fimc_register_capture_device(fimc, &fmd->v4l2_dev); |
| 302 | if (!ret) |
| 303 | fimc->vid_cap.user_subdev_api = fmd->user_subdev_api; |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static int csis_register_callback(struct device *dev, void *p) |
| 308 | { |
| 309 | struct v4l2_subdev *sd = dev_get_drvdata(dev); |
| 310 | struct platform_device *pdev; |
| 311 | struct fimc_md *fmd = p; |
| 312 | int id, ret; |
| 313 | |
| 314 | if (!sd) |
| 315 | return 0; |
| 316 | pdev = v4l2_get_subdevdata(sd); |
| 317 | if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES) |
| 318 | return 0; |
| 319 | v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name); |
| 320 | |
| 321 | id = pdev->id < 0 ? 0 : pdev->id; |
| 322 | fmd->csis[id].sd = sd; |
| 323 | sd->grp_id = CSIS_GROUP_ID; |
| 324 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
| 325 | if (ret) |
| 326 | v4l2_err(&fmd->v4l2_dev, |
| 327 | "Failed to register CSIS subdevice: %d\n", ret); |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * fimc_md_register_platform_entities - register FIMC and CSIS media entities |
| 333 | */ |
| 334 | static int fimc_md_register_platform_entities(struct fimc_md *fmd) |
| 335 | { |
| 336 | struct device_driver *driver; |
| 337 | int ret; |
| 338 | |
| 339 | driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type); |
| 340 | if (!driver) |
| 341 | return -ENODEV; |
| 342 | ret = driver_for_each_device(driver, NULL, fmd, |
| 343 | fimc_register_callback); |
| 344 | put_driver(driver); |
| 345 | if (ret) |
| 346 | return ret; |
| 347 | |
| 348 | driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type); |
| 349 | if (driver) { |
| 350 | ret = driver_for_each_device(driver, NULL, fmd, |
| 351 | csis_register_callback); |
| 352 | put_driver(driver); |
| 353 | } |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static void fimc_md_unregister_entities(struct fimc_md *fmd) |
| 358 | { |
| 359 | int i; |
| 360 | |
| 361 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 362 | if (fmd->fimc[i] == NULL) |
| 363 | continue; |
| 364 | fimc_unregister_m2m_device(fmd->fimc[i]); |
| 365 | fimc_unregister_capture_device(fmd->fimc[i]); |
| 366 | fmd->fimc[i] = NULL; |
| 367 | } |
| 368 | for (i = 0; i < CSIS_MAX_ENTITIES; i++) { |
| 369 | if (fmd->csis[i].sd == NULL) |
| 370 | continue; |
| 371 | v4l2_device_unregister_subdev(fmd->csis[i].sd); |
| 372 | fmd->csis[i].sd = NULL; |
| 373 | } |
| 374 | for (i = 0; i < fmd->num_sensors; i++) { |
| 375 | if (fmd->sensor[i].subdev == NULL) |
| 376 | continue; |
| 377 | fimc_md_unregister_sensor(fmd->sensor[i].subdev); |
| 378 | fmd->sensor[i].subdev = NULL; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static int fimc_md_register_video_nodes(struct fimc_md *fmd) |
| 383 | { |
| 384 | int i, ret = 0; |
| 385 | |
| 386 | for (i = 0; i < FIMC_MAX_DEVS && !ret; i++) { |
| 387 | if (!fmd->fimc[i]) |
| 388 | continue; |
| 389 | |
| 390 | if (fmd->fimc[i]->m2m.vfd) |
| 391 | ret = video_register_device(fmd->fimc[i]->m2m.vfd, |
| 392 | VFL_TYPE_GRABBER, -1); |
| 393 | if (ret) |
| 394 | break; |
| 395 | if (fmd->fimc[i]->vid_cap.vfd) |
| 396 | ret = video_register_device(fmd->fimc[i]->vid_cap.vfd, |
| 397 | VFL_TYPE_GRABBER, -1); |
| 398 | } |
| 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * __fimc_md_create_fimc_links - create links to all FIMC entities |
| 405 | * @fmd: fimc media device |
| 406 | * @source: the source entity to create links to all fimc entities from |
| 407 | * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null |
| 408 | * @pad: the source entity pad index |
| 409 | * @fimc_id: index of the fimc device for which link should be enabled |
| 410 | */ |
| 411 | static int __fimc_md_create_fimc_links(struct fimc_md *fmd, |
| 412 | struct media_entity *source, |
| 413 | struct v4l2_subdev *sensor, |
| 414 | int pad, int fimc_id) |
| 415 | { |
| 416 | struct fimc_sensor_info *s_info; |
| 417 | struct media_entity *sink; |
| 418 | unsigned int flags; |
| 419 | int ret, i, src_pad; |
| 420 | |
| 421 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 422 | if (!fmd->fimc[i]) |
| 423 | break; |
| 424 | /* |
| 425 | * Some FIMC variants are not fitted with camera capture |
| 426 | * interface. Skip creating a link from sensor for those. |
| 427 | */ |
| 428 | if (sensor && sensor->grp_id == SENSOR_GROUP_ID && |
| 429 | !fmd->fimc[i]->variant->has_cam_if) |
| 430 | continue; |
| 431 | |
| 432 | flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0; |
| 433 | sink = &fmd->fimc[i]->vid_cap.vfd->entity; |
| 434 | ret = media_entity_create_link(source, 0, sink, 0, flags); |
| 435 | if (ret) |
| 436 | return ret; |
| 437 | |
| 438 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]", |
| 439 | source->name, flags ? '=' : '-', sink->name); |
| 440 | |
| 441 | if (flags == 0 || sensor == NULL) |
| 442 | continue; |
| 443 | s_info = v4l2_get_subdev_hostdata(sensor); |
| 444 | if (!WARN_ON(s_info == NULL)) { |
| 445 | unsigned long irq_flags; |
| 446 | spin_lock_irqsave(&fmd->slock, irq_flags); |
| 447 | s_info->host = fmd->fimc[i]; |
| 448 | spin_unlock_irqrestore(&fmd->slock, irq_flags); |
| 449 | } |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * fimc_md_create_links - create default links between registered entities |
| 456 | * |
| 457 | * Parallel interface sensor entities are connected directly to FIMC capture |
| 458 | * entities. The sensors using MIPI CSIS bus are connected through immutable |
| 459 | * link with CSI receiver entity specified by mux_id. Any registered CSIS |
| 460 | * entity has a link to each registered FIMC capture entity. Enabled links |
| 461 | * are created by default between each subsequent registered sensor and |
| 462 | * subsequent FIMC capture entity. The number of default active links is |
| 463 | * determined by the number of available sensors or FIMC entities, |
| 464 | * whichever is less. |
| 465 | */ |
| 466 | static int fimc_md_create_links(struct fimc_md *fmd) |
| 467 | { |
| 468 | struct v4l2_subdev *sensor, *csis; |
| 469 | struct s5p_fimc_isp_info *pdata; |
| 470 | struct fimc_sensor_info *s_info; |
| 471 | struct media_entity *source; |
| 472 | int fimc_id = 0; |
| 473 | int i, pad; |
| 474 | int ret = 0; |
| 475 | |
| 476 | for (i = 0; i < fmd->num_sensors; i++) { |
| 477 | if (fmd->sensor[i].subdev == NULL) |
| 478 | continue; |
| 479 | |
| 480 | sensor = fmd->sensor[i].subdev; |
| 481 | s_info = v4l2_get_subdev_hostdata(sensor); |
| 482 | if (!s_info || !s_info->pdata) |
| 483 | continue; |
| 484 | |
| 485 | source = NULL; |
| 486 | pdata = s_info->pdata; |
| 487 | |
| 488 | switch (pdata->bus_type) { |
| 489 | case FIMC_MIPI_CSI2: |
| 490 | if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES, |
| 491 | "Wrong CSI channel id: %d\n", pdata->mux_id)) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | csis = fmd->csis[pdata->mux_id].sd; |
| 495 | if (WARN(csis == NULL, |
| 496 | "MIPI-CSI interface specified " |
| 497 | "but s5p-csis module is not loaded!\n")) |
| 498 | continue; |
| 499 | |
| 500 | ret = media_entity_create_link(&sensor->entity, 0, |
| 501 | &csis->entity, CSIS_PAD_SINK, |
| 502 | MEDIA_LNK_FL_IMMUTABLE | |
| 503 | MEDIA_LNK_FL_ENABLED); |
| 504 | if (ret) |
| 505 | return ret; |
| 506 | |
| 507 | v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]", |
| 508 | sensor->entity.name, csis->entity.name); |
| 509 | |
| 510 | sensor = NULL; |
| 511 | source = &csis->entity; |
| 512 | pad = CSIS_PAD_SOURCE; |
| 513 | break; |
| 514 | |
| 515 | case FIMC_ITU_601...FIMC_ITU_656: |
| 516 | source = &sensor->entity; |
| 517 | pad = 0; |
| 518 | break; |
| 519 | |
| 520 | default: |
| 521 | v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n", |
| 522 | pdata->bus_type); |
| 523 | return -EINVAL; |
| 524 | } |
| 525 | if (source == NULL) |
| 526 | continue; |
| 527 | |
| 528 | ret = __fimc_md_create_fimc_links(fmd, source, sensor, pad, |
| 529 | fimc_id++); |
| 530 | } |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * The peripheral sensor clock management. |
| 536 | */ |
| 537 | static int fimc_md_get_clocks(struct fimc_md *fmd) |
| 538 | { |
| 539 | char clk_name[32]; |
| 540 | struct clk *clock; |
| 541 | int i; |
| 542 | |
| 543 | for (i = 0; i < FIMC_MAX_CAMCLKS; i++) { |
| 544 | snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i); |
| 545 | clock = clk_get(NULL, clk_name); |
| 546 | if (IS_ERR_OR_NULL(clock)) { |
| 547 | v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s", |
| 548 | clk_name); |
| 549 | return -ENXIO; |
| 550 | } |
| 551 | fmd->camclk[i].clock = clock; |
| 552 | } |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static void fimc_md_put_clocks(struct fimc_md *fmd) |
| 557 | { |
| 558 | int i = FIMC_MAX_CAMCLKS; |
| 559 | |
| 560 | while (--i >= 0) { |
| 561 | if (IS_ERR_OR_NULL(fmd->camclk[i].clock)) |
| 562 | continue; |
| 563 | clk_put(fmd->camclk[i].clock); |
| 564 | fmd->camclk[i].clock = NULL; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | static int __fimc_md_set_camclk(struct fimc_md *fmd, |
| 569 | struct fimc_sensor_info *s_info, |
| 570 | bool on) |
| 571 | { |
| 572 | struct s5p_fimc_isp_info *pdata = s_info->pdata; |
| 573 | struct fimc_camclk_info *camclk; |
| 574 | int ret = 0; |
| 575 | |
| 576 | if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL) |
| 577 | return -EINVAL; |
| 578 | |
| 579 | if (s_info->clk_on == on) |
| 580 | return 0; |
| 581 | camclk = &fmd->camclk[pdata->clk_id]; |
| 582 | |
| 583 | dbg("camclk %d, f: %lu, clk: %p, on: %d", |
| 584 | pdata->clk_id, pdata->clk_frequency, camclk, on); |
| 585 | |
| 586 | if (on) { |
| 587 | if (camclk->use_count > 0 && |
| 588 | camclk->frequency != pdata->clk_frequency) |
| 589 | return -EINVAL; |
| 590 | |
| 591 | if (camclk->use_count++ == 0) { |
| 592 | clk_set_rate(camclk->clock, pdata->clk_frequency); |
| 593 | camclk->frequency = pdata->clk_frequency; |
| 594 | ret = clk_enable(camclk->clock); |
| 595 | } |
| 596 | s_info->clk_on = 1; |
| 597 | dbg("Enabled camclk %d: f: %lu", pdata->clk_id, |
| 598 | clk_get_rate(camclk->clock)); |
| 599 | |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | if (WARN_ON(camclk->use_count == 0)) |
| 604 | return 0; |
| 605 | |
| 606 | if (--camclk->use_count == 0) { |
| 607 | clk_disable(camclk->clock); |
| 608 | s_info->clk_on = 0; |
| 609 | dbg("Disabled camclk %d", pdata->clk_id); |
| 610 | } |
| 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * fimc_md_set_camclk - peripheral sensor clock setup |
| 616 | * @sd: sensor subdev to configure sclk_cam clock for |
| 617 | * @on: 1 to enable or 0 to disable the clock |
| 618 | * |
| 619 | * There are 2 separate clock outputs available in the SoC for external |
| 620 | * image processors. These clocks are shared between all registered FIMC |
| 621 | * devices to which sensors can be attached, either directly or through |
| 622 | * the MIPI CSI receiver. The clock is allowed here to be used by |
| 623 | * multiple sensors concurrently if they use same frequency. |
| 624 | * The per sensor subdev clk_on attribute helps to synchronize accesses |
| 625 | * to the sclk_cam clocks from the video and media device nodes. |
| 626 | * This function should only be called when the graph mutex is held. |
| 627 | */ |
| 628 | int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on) |
| 629 | { |
| 630 | struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd); |
| 631 | struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity); |
| 632 | |
| 633 | return __fimc_md_set_camclk(fmd, s_info, on); |
| 634 | } |
| 635 | |
| 636 | static int fimc_md_link_notify(struct media_pad *source, |
| 637 | struct media_pad *sink, u32 flags) |
| 638 | { |
| 639 | struct video_device *vid_dev; |
| 640 | struct fimc_dev *fimc; |
| 641 | int ret = 0; |
| 642 | |
| 643 | if (WARN_ON(media_entity_type(sink->entity) != MEDIA_ENT_T_DEVNODE)) |
| 644 | return 0; |
| 645 | |
| 646 | vid_dev = media_entity_to_video_device(sink->entity); |
| 647 | fimc = video_get_drvdata(vid_dev); |
| 648 | |
| 649 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { |
| 650 | ret = __fimc_pipeline_shutdown(fimc); |
| 651 | fimc->pipeline.sensor = NULL; |
| 652 | fimc->pipeline.csis = NULL; |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame^] | 653 | |
| 654 | mutex_lock(&fimc->lock); |
| 655 | fimc_ctrls_delete(fimc->vid_cap.ctx); |
| 656 | mutex_unlock(&fimc->lock); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 657 | return ret; |
| 658 | } |
| 659 | /* |
| 660 | * Link activation. Enable power of pipeline elements only if the |
| 661 | * pipeline is already in use, i.e. its video node is opened. |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame^] | 662 | * Recreate the controls destroyed during the link deactivation. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 663 | */ |
| 664 | mutex_lock(&fimc->lock); |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame^] | 665 | if (fimc->vid_cap.refcnt > 0) { |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 666 | ret = __fimc_pipeline_initialize(fimc, source->entity, true); |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame^] | 667 | if (!ret) |
| 668 | ret = fimc_capture_ctrls_create(fimc); |
| 669 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 670 | mutex_unlock(&fimc->lock); |
| 671 | |
| 672 | return ret ? -EPIPE : ret; |
| 673 | } |
| 674 | |
| 675 | static ssize_t fimc_md_sysfs_show(struct device *dev, |
| 676 | struct device_attribute *attr, char *buf) |
| 677 | { |
| 678 | struct platform_device *pdev = to_platform_device(dev); |
| 679 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 680 | |
| 681 | if (fmd->user_subdev_api) |
| 682 | return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE); |
| 683 | |
| 684 | return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE); |
| 685 | } |
| 686 | |
| 687 | static ssize_t fimc_md_sysfs_store(struct device *dev, |
| 688 | struct device_attribute *attr, |
| 689 | const char *buf, size_t count) |
| 690 | { |
| 691 | struct platform_device *pdev = to_platform_device(dev); |
| 692 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 693 | bool subdev_api; |
| 694 | int i; |
| 695 | |
| 696 | if (!strcmp(buf, "vid-dev\n")) |
| 697 | subdev_api = false; |
| 698 | else if (!strcmp(buf, "sub-dev\n")) |
| 699 | subdev_api = true; |
| 700 | else |
| 701 | return count; |
| 702 | |
| 703 | fmd->user_subdev_api = subdev_api; |
| 704 | for (i = 0; i < FIMC_MAX_DEVS; i++) |
| 705 | if (fmd->fimc[i]) |
| 706 | fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api; |
| 707 | return count; |
| 708 | } |
| 709 | /* |
| 710 | * This device attribute is to select video pipeline configuration method. |
| 711 | * There are following valid values: |
| 712 | * vid-dev - for V4L2 video node API only, subdevice will be configured |
| 713 | * by the host driver. |
| 714 | * sub-dev - for media controller API, subdevs must be configured in user |
| 715 | * space before starting streaming. |
| 716 | */ |
| 717 | static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO, |
| 718 | fimc_md_sysfs_show, fimc_md_sysfs_store); |
| 719 | |
| 720 | static int __devinit fimc_md_probe(struct platform_device *pdev) |
| 721 | { |
| 722 | struct v4l2_device *v4l2_dev; |
| 723 | struct fimc_md *fmd; |
| 724 | int ret; |
| 725 | |
| 726 | if (WARN(!pdev->dev.platform_data, "Platform data not specified!\n")) |
| 727 | return -EINVAL; |
| 728 | |
| 729 | fmd = kzalloc(sizeof(struct fimc_md), GFP_KERNEL); |
| 730 | if (!fmd) |
| 731 | return -ENOMEM; |
| 732 | |
| 733 | spin_lock_init(&fmd->slock); |
| 734 | fmd->pdev = pdev; |
| 735 | |
| 736 | strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC", |
| 737 | sizeof(fmd->media_dev.model)); |
| 738 | fmd->media_dev.link_notify = fimc_md_link_notify; |
| 739 | fmd->media_dev.dev = &pdev->dev; |
| 740 | |
| 741 | v4l2_dev = &fmd->v4l2_dev; |
| 742 | v4l2_dev->mdev = &fmd->media_dev; |
| 743 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s", |
| 744 | dev_name(&pdev->dev)); |
| 745 | |
| 746 | ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev); |
| 747 | if (ret < 0) { |
| 748 | v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret); |
| 749 | goto err1; |
| 750 | } |
| 751 | ret = media_device_register(&fmd->media_dev); |
| 752 | if (ret < 0) { |
| 753 | v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret); |
| 754 | goto err2; |
| 755 | } |
| 756 | ret = fimc_md_get_clocks(fmd); |
| 757 | if (ret) |
| 758 | goto err3; |
| 759 | |
| 760 | fmd->user_subdev_api = false; |
| 761 | ret = fimc_md_register_platform_entities(fmd); |
| 762 | if (ret) |
| 763 | goto err3; |
| 764 | |
| 765 | ret = fimc_md_register_sensor_entities(fmd); |
| 766 | if (ret) |
| 767 | goto err3; |
| 768 | ret = fimc_md_create_links(fmd); |
| 769 | if (ret) |
| 770 | goto err3; |
| 771 | ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev); |
| 772 | if (ret) |
| 773 | goto err3; |
| 774 | ret = fimc_md_register_video_nodes(fmd); |
| 775 | if (ret) |
| 776 | goto err3; |
| 777 | |
| 778 | ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode); |
| 779 | if (!ret) { |
| 780 | platform_set_drvdata(pdev, fmd); |
| 781 | return 0; |
| 782 | } |
| 783 | err3: |
| 784 | media_device_unregister(&fmd->media_dev); |
| 785 | fimc_md_put_clocks(fmd); |
| 786 | fimc_md_unregister_entities(fmd); |
| 787 | err2: |
| 788 | v4l2_device_unregister(&fmd->v4l2_dev); |
| 789 | err1: |
| 790 | kfree(fmd); |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | static int __devexit fimc_md_remove(struct platform_device *pdev) |
| 795 | { |
| 796 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 797 | |
| 798 | if (!fmd) |
| 799 | return 0; |
| 800 | device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode); |
| 801 | fimc_md_unregister_entities(fmd); |
| 802 | media_device_unregister(&fmd->media_dev); |
| 803 | fimc_md_put_clocks(fmd); |
| 804 | kfree(fmd); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static struct platform_driver fimc_md_driver = { |
| 809 | .probe = fimc_md_probe, |
| 810 | .remove = __devexit_p(fimc_md_remove), |
| 811 | .driver = { |
| 812 | .name = "s5p-fimc-md", |
| 813 | .owner = THIS_MODULE, |
| 814 | } |
| 815 | }; |
| 816 | |
| 817 | int __init fimc_md_init(void) |
| 818 | { |
| 819 | int ret; |
| 820 | request_module("s5p-csis"); |
| 821 | ret = fimc_register_driver(); |
| 822 | if (ret) |
| 823 | return ret; |
| 824 | return platform_driver_register(&fimc_md_driver); |
| 825 | } |
| 826 | void __exit fimc_md_exit(void) |
| 827 | { |
| 828 | platform_driver_unregister(&fimc_md_driver); |
| 829 | fimc_unregister_driver(); |
| 830 | } |
| 831 | |
| 832 | module_init(fimc_md_init); |
| 833 | module_exit(fimc_md_exit); |
| 834 | |
| 835 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); |
| 836 | MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver"); |
| 837 | MODULE_LICENSE("GPL"); |
| 838 | MODULE_VERSION("2.0.1"); |