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> |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 24 | #include <media/v4l2-ctrls.h> |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 25 | #include <media/media-device.h> |
| 26 | |
| 27 | #include "fimc-core.h" |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 28 | #include "fimc-lite.h" |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 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 | */ |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 41 | void fimc_pipeline_prepare(struct fimc_pipeline *p, struct media_entity *me) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 42 | { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 43 | struct media_pad *pad = &me->pads[0]; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 44 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 45 | int i; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 46 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 47 | for (i = 0; i < IDX_MAX; i++) |
| 48 | p->subdevs[i] = NULL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 49 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 50 | while (1) { |
| 51 | if (!(pad->flags & MEDIA_PAD_FL_SINK)) |
| 52 | break; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 53 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 54 | /* source pad */ |
| 55 | pad = media_entity_remote_source(pad); |
| 56 | if (pad == NULL || |
| 57 | media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
| 58 | break; |
| 59 | |
| 60 | sd = media_entity_to_v4l2_subdev(pad->entity); |
| 61 | |
| 62 | switch (sd->grp_id) { |
| 63 | case SENSOR_GROUP_ID: |
| 64 | p->subdevs[IDX_SENSOR] = sd; |
| 65 | break; |
| 66 | case CSIS_GROUP_ID: |
| 67 | p->subdevs[IDX_CSIS] = sd; |
| 68 | break; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 69 | case FLITE_GROUP_ID: |
| 70 | p->subdevs[IDX_FLITE] = sd; |
| 71 | break; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 72 | case FIMC_GROUP_ID: |
| 73 | /* No need to control FIMC subdev through subdev ops */ |
| 74 | break; |
| 75 | default: |
| 76 | pr_warn("%s: Unknown subdev grp_id: %#x\n", |
| 77 | __func__, sd->grp_id); |
| 78 | } |
| 79 | /* sink pad */ |
| 80 | pad = &sd->entity.pads[0]; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * __subdev_set_power - change power state of a single subdev |
| 86 | * @sd: subdevice to change power state for |
| 87 | * @on: 1 to enable power or 0 to disable |
| 88 | * |
| 89 | * Return result of s_power subdev operation or -ENXIO if sd argument |
| 90 | * is NULL. Return 0 if the subdevice does not implement s_power. |
| 91 | */ |
| 92 | static int __subdev_set_power(struct v4l2_subdev *sd, int on) |
| 93 | { |
| 94 | int *use_count; |
| 95 | int ret; |
| 96 | |
| 97 | if (sd == NULL) |
| 98 | return -ENXIO; |
| 99 | |
| 100 | use_count = &sd->entity.use_count; |
| 101 | if (on && (*use_count)++ > 0) |
| 102 | return 0; |
| 103 | else if (!on && (*use_count == 0 || --(*use_count) > 0)) |
| 104 | return 0; |
| 105 | ret = v4l2_subdev_call(sd, core, s_power, on); |
| 106 | |
| 107 | return ret != -ENOIOCTLCMD ? ret : 0; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * fimc_pipeline_s_power - change power state of all pipeline subdevs |
| 112 | * @fimc: fimc device terminating the pipeline |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 113 | * @state: true to power on, false to power off |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 114 | * |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 115 | * Needs to be called with the graph mutex held. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 116 | */ |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 117 | int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 118 | { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 119 | unsigned int i; |
| 120 | int ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 121 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 122 | if (p->subdevs[IDX_SENSOR] == NULL) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 123 | return -ENXIO; |
| 124 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 125 | for (i = 0; i < IDX_MAX; i++) { |
| 126 | unsigned int idx = state ? (IDX_MAX - 1) - i : i; |
| 127 | |
| 128 | ret = __subdev_set_power(p->subdevs[idx], state); |
| 129 | if (ret < 0 && ret != -ENXIO) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 130 | return ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 131 | } |
| 132 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 133 | return 0; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * __fimc_pipeline_initialize - update the pipeline information, enable power |
| 138 | * of all pipeline subdevs and the sensor clock |
| 139 | * @me: media entity to start graph walk with |
| 140 | * @prep: true to acquire sensor (and csis) subdevs |
| 141 | * |
| 142 | * This function must be called with the graph mutex held. |
| 143 | */ |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 144 | static int __fimc_pipeline_initialize(struct fimc_pipeline *p, |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 145 | struct media_entity *me, bool prep) |
| 146 | { |
| 147 | int ret; |
| 148 | |
| 149 | if (prep) |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 150 | fimc_pipeline_prepare(p, me); |
| 151 | |
| 152 | if (p->subdevs[IDX_SENSOR] == NULL) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 153 | return -EINVAL; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 154 | |
| 155 | ret = fimc_md_set_camclk(p->subdevs[IDX_SENSOR], true); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 156 | if (ret) |
| 157 | return ret; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 158 | |
| 159 | return fimc_pipeline_s_power(p, 1); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 160 | } |
| 161 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 162 | int fimc_pipeline_initialize(struct fimc_pipeline *p, struct media_entity *me, |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 163 | bool prep) |
| 164 | { |
| 165 | int ret; |
| 166 | |
| 167 | mutex_lock(&me->parent->graph_mutex); |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 168 | ret = __fimc_pipeline_initialize(p, me, prep); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 169 | mutex_unlock(&me->parent->graph_mutex); |
| 170 | |
| 171 | return ret; |
| 172 | } |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 173 | EXPORT_SYMBOL_GPL(fimc_pipeline_initialize); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * __fimc_pipeline_shutdown - disable the sensor clock and pipeline power |
| 177 | * @fimc: fimc device terminating the pipeline |
| 178 | * |
| 179 | * Disable power of all subdevs in the pipeline and turn off the external |
| 180 | * sensor clock. |
| 181 | * Called with the graph mutex held. |
| 182 | */ |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 183 | int __fimc_pipeline_shutdown(struct fimc_pipeline *p) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 184 | { |
| 185 | int ret = 0; |
| 186 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 187 | if (p->subdevs[IDX_SENSOR]) { |
| 188 | ret = fimc_pipeline_s_power(p, 0); |
| 189 | fimc_md_set_camclk(p->subdevs[IDX_SENSOR], false); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 190 | } |
| 191 | return ret == -ENXIO ? 0 : ret; |
| 192 | } |
| 193 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 194 | int fimc_pipeline_shutdown(struct fimc_pipeline *p) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 195 | { |
Sylwester Nawrocki | 90e614bb | 2012-05-16 11:35:04 -0300 | [diff] [blame^] | 196 | struct media_entity *me; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 197 | int ret; |
| 198 | |
Sylwester Nawrocki | 90e614bb | 2012-05-16 11:35:04 -0300 | [diff] [blame^] | 199 | if (!p || !p->subdevs[IDX_SENSOR]) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | me = &p->subdevs[IDX_SENSOR]->entity; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 203 | mutex_lock(&me->parent->graph_mutex); |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 204 | ret = __fimc_pipeline_shutdown(p); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 205 | mutex_unlock(&me->parent->graph_mutex); |
| 206 | |
| 207 | return ret; |
| 208 | } |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(fimc_pipeline_shutdown); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 213 | * @pipeline: video pipeline structure |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 214 | * @on: passed as the s_stream call argument |
| 215 | */ |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 216 | int fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 217 | { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 218 | int i, ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 219 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 220 | if (p->subdevs[IDX_SENSOR] == NULL) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 221 | return -ENODEV; |
| 222 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 223 | for (i = 0; i < IDX_MAX; i++) { |
| 224 | unsigned int idx = on ? (IDX_MAX - 1) - i : i; |
| 225 | |
| 226 | ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on); |
| 227 | |
| 228 | if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 234 | } |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 235 | EXPORT_SYMBOL_GPL(fimc_pipeline_s_stream); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * Sensor subdevice helper functions |
| 239 | */ |
| 240 | static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd, |
| 241 | struct fimc_sensor_info *s_info) |
| 242 | { |
| 243 | struct i2c_adapter *adapter; |
| 244 | struct v4l2_subdev *sd = NULL; |
| 245 | |
| 246 | if (!s_info || !fmd) |
| 247 | return NULL; |
| 248 | |
| 249 | adapter = i2c_get_adapter(s_info->pdata->i2c_bus_num); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 250 | if (!adapter) { |
| 251 | v4l2_warn(&fmd->v4l2_dev, |
| 252 | "Failed to get I2C adapter %d, deferring probe\n", |
| 253 | s_info->pdata->i2c_bus_num); |
| 254 | return ERR_PTR(-EPROBE_DEFER); |
| 255 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 256 | sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter, |
| 257 | s_info->pdata->board_info, NULL); |
| 258 | if (IS_ERR_OR_NULL(sd)) { |
Sylwester Nawrocki | 7acde02 | 2011-09-13 14:07:09 -0300 | [diff] [blame] | 259 | i2c_put_adapter(adapter); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 260 | v4l2_warn(&fmd->v4l2_dev, |
| 261 | "Failed to acquire subdev %s, deferring probe\n", |
| 262 | s_info->pdata->board_info->type); |
| 263 | return ERR_PTR(-EPROBE_DEFER); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 264 | } |
| 265 | v4l2_set_subdev_hostdata(sd, s_info); |
| 266 | sd->grp_id = SENSOR_GROUP_ID; |
| 267 | |
| 268 | v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n", |
| 269 | s_info->pdata->board_info->type); |
| 270 | return sd; |
| 271 | } |
| 272 | |
| 273 | static void fimc_md_unregister_sensor(struct v4l2_subdev *sd) |
| 274 | { |
| 275 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Sylwester Nawrocki | 7acde02 | 2011-09-13 14:07:09 -0300 | [diff] [blame] | 276 | struct i2c_adapter *adapter; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 277 | |
| 278 | if (!client) |
| 279 | return; |
| 280 | v4l2_device_unregister_subdev(sd); |
Sylwester Nawrocki | 7acde02 | 2011-09-13 14:07:09 -0300 | [diff] [blame] | 281 | adapter = client->adapter; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 282 | i2c_unregister_device(client); |
Sylwester Nawrocki | 7acde02 | 2011-09-13 14:07:09 -0300 | [diff] [blame] | 283 | if (adapter) |
| 284 | i2c_put_adapter(adapter); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int fimc_md_register_sensor_entities(struct fimc_md *fmd) |
| 288 | { |
| 289 | struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data; |
| 290 | struct fimc_dev *fd = NULL; |
| 291 | int num_clients, ret, i; |
| 292 | |
| 293 | /* |
| 294 | * Runtime resume one of the FIMC entities to make sure |
| 295 | * the sclk_cam clocks are not globally disabled. |
| 296 | */ |
| 297 | for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++) |
| 298 | if (fmd->fimc[i]) |
| 299 | fd = fmd->fimc[i]; |
| 300 | if (!fd) |
| 301 | return -ENXIO; |
| 302 | ret = pm_runtime_get_sync(&fd->pdev->dev); |
| 303 | if (ret < 0) |
| 304 | return ret; |
| 305 | |
| 306 | WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor)); |
| 307 | num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor)); |
| 308 | |
| 309 | fmd->num_sensors = num_clients; |
| 310 | for (i = 0; i < num_clients; i++) { |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 311 | struct v4l2_subdev *sd; |
| 312 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 313 | fmd->sensor[i].pdata = &pdata->isp_info[i]; |
| 314 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true); |
| 315 | if (ret) |
| 316 | break; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 317 | sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 318 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 319 | |
| 320 | if (!IS_ERR(sd)) { |
| 321 | fmd->sensor[i].subdev = sd; |
| 322 | } else { |
| 323 | fmd->sensor[i].subdev = NULL; |
| 324 | ret = PTR_ERR(sd); |
| 325 | break; |
| 326 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 327 | if (ret) |
| 328 | break; |
| 329 | } |
| 330 | pm_runtime_put(&fd->pdev->dev); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * MIPI CSIS and FIMC platform devices registration. |
| 336 | */ |
| 337 | static int fimc_register_callback(struct device *dev, void *p) |
| 338 | { |
| 339 | struct fimc_dev *fimc = dev_get_drvdata(dev); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 340 | struct v4l2_subdev *sd = &fimc->vid_cap.subdev; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 341 | struct fimc_md *fmd = p; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 342 | int ret = 0; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 343 | |
| 344 | if (!fimc || !fimc->pdev) |
| 345 | return 0; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 346 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 347 | if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS) |
| 348 | return 0; |
| 349 | |
| 350 | fmd->fimc[fimc->pdev->id] = fimc; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 351 | sd->grp_id = FIMC_GROUP_ID; |
| 352 | |
| 353 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
| 354 | if (ret) { |
| 355 | v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n", |
| 356 | fimc->id, ret); |
| 357 | } |
| 358 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 359 | return ret; |
| 360 | } |
| 361 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 362 | static int fimc_lite_register_callback(struct device *dev, void *p) |
| 363 | { |
| 364 | struct fimc_lite *fimc = dev_get_drvdata(dev); |
| 365 | struct v4l2_subdev *sd = &fimc->subdev; |
| 366 | struct fimc_md *fmd = p; |
| 367 | int ret; |
| 368 | |
| 369 | if (fimc == NULL) |
| 370 | return 0; |
| 371 | |
| 372 | if (fimc->index >= FIMC_LITE_MAX_DEVS) |
| 373 | return 0; |
| 374 | |
| 375 | fmd->fimc_lite[fimc->index] = fimc; |
| 376 | sd->grp_id = FLITE_GROUP_ID; |
| 377 | |
| 378 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
| 379 | if (ret) { |
| 380 | v4l2_err(&fmd->v4l2_dev, |
| 381 | "Failed to register FIMC-LITE.%d (%d)\n", |
| 382 | fimc->index, ret); |
| 383 | } |
| 384 | return ret; |
| 385 | } |
| 386 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 387 | static int csis_register_callback(struct device *dev, void *p) |
| 388 | { |
| 389 | struct v4l2_subdev *sd = dev_get_drvdata(dev); |
| 390 | struct platform_device *pdev; |
| 391 | struct fimc_md *fmd = p; |
| 392 | int id, ret; |
| 393 | |
| 394 | if (!sd) |
| 395 | return 0; |
| 396 | pdev = v4l2_get_subdevdata(sd); |
| 397 | if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES) |
| 398 | return 0; |
| 399 | v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name); |
| 400 | |
| 401 | id = pdev->id < 0 ? 0 : pdev->id; |
| 402 | fmd->csis[id].sd = sd; |
| 403 | sd->grp_id = CSIS_GROUP_ID; |
| 404 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
| 405 | if (ret) |
| 406 | v4l2_err(&fmd->v4l2_dev, |
| 407 | "Failed to register CSIS subdevice: %d\n", ret); |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * fimc_md_register_platform_entities - register FIMC and CSIS media entities |
| 413 | */ |
| 414 | static int fimc_md_register_platform_entities(struct fimc_md *fmd) |
| 415 | { |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 416 | struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 417 | struct device_driver *driver; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 418 | int ret, i; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 419 | |
| 420 | driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 421 | if (!driver) { |
| 422 | v4l2_warn(&fmd->v4l2_dev, |
| 423 | "%s driver not found, deffering probe\n", |
| 424 | FIMC_MODULE_NAME); |
| 425 | return -EPROBE_DEFER; |
| 426 | } |
| 427 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 428 | ret = driver_for_each_device(driver, NULL, fmd, |
| 429 | fimc_register_callback); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 430 | if (ret) |
| 431 | return ret; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 432 | |
| 433 | driver = driver_find(FIMC_LITE_DRV_NAME, &platform_bus_type); |
| 434 | if (driver && try_module_get(driver->owner)) { |
| 435 | ret = driver_for_each_device(driver, NULL, fmd, |
| 436 | fimc_lite_register_callback); |
| 437 | if (ret) |
| 438 | return ret; |
| 439 | module_put(driver->owner); |
| 440 | } |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 441 | /* |
| 442 | * Check if there is any sensor on the MIPI-CSI2 bus and |
| 443 | * if not skip the s5p-csis module loading. |
| 444 | */ |
Sylwester Nawrocki | 41df5bf | 2012-04-30 12:10:58 -0300 | [diff] [blame] | 445 | if (pdata == NULL) |
| 446 | return 0; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 447 | for (i = 0; i < pdata->num_clients; i++) { |
| 448 | if (pdata->isp_info[i].bus_type == FIMC_MIPI_CSI2) { |
| 449 | ret = 1; |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | if (!ret) |
| 454 | return 0; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 455 | |
| 456 | driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 457 | if (!driver || !try_module_get(driver->owner)) { |
| 458 | v4l2_warn(&fmd->v4l2_dev, |
| 459 | "%s driver not found, deffering probe\n", |
| 460 | CSIS_DRIVER_NAME); |
| 461 | return -EPROBE_DEFER; |
| 462 | } |
| 463 | |
| 464 | return driver_for_each_device(driver, NULL, fmd, |
| 465 | csis_register_callback); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static void fimc_md_unregister_entities(struct fimc_md *fmd) |
| 469 | { |
| 470 | int i; |
| 471 | |
| 472 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 473 | if (fmd->fimc[i] == NULL) |
| 474 | continue; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 475 | v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 476 | fmd->fimc[i] = NULL; |
| 477 | } |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 478 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { |
| 479 | if (fmd->fimc_lite[i] == NULL) |
| 480 | continue; |
| 481 | v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev); |
| 482 | fmd->fimc_lite[i] = NULL; |
| 483 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 484 | for (i = 0; i < CSIS_MAX_ENTITIES; i++) { |
| 485 | if (fmd->csis[i].sd == NULL) |
| 486 | continue; |
| 487 | v4l2_device_unregister_subdev(fmd->csis[i].sd); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 488 | module_put(fmd->csis[i].sd->owner); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 489 | fmd->csis[i].sd = NULL; |
| 490 | } |
| 491 | for (i = 0; i < fmd->num_sensors; i++) { |
| 492 | if (fmd->sensor[i].subdev == NULL) |
| 493 | continue; |
| 494 | fimc_md_unregister_sensor(fmd->sensor[i].subdev); |
| 495 | fmd->sensor[i].subdev = NULL; |
| 496 | } |
| 497 | } |
| 498 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 499 | /** |
| 500 | * __fimc_md_create_fimc_links - create links to all FIMC entities |
| 501 | * @fmd: fimc media device |
| 502 | * @source: the source entity to create links to all fimc entities from |
| 503 | * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null |
| 504 | * @pad: the source entity pad index |
| 505 | * @fimc_id: index of the fimc device for which link should be enabled |
| 506 | */ |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 507 | static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd, |
| 508 | struct media_entity *source, |
| 509 | struct v4l2_subdev *sensor, |
| 510 | int pad, int fimc_id) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 511 | { |
| 512 | struct fimc_sensor_info *s_info; |
| 513 | struct media_entity *sink; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 514 | unsigned int flags = 0; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 515 | int ret, i; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 516 | |
| 517 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 518 | if (!fmd->fimc[i]) |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 519 | continue; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 520 | /* |
| 521 | * Some FIMC variants are not fitted with camera capture |
| 522 | * interface. Skip creating a link from sensor for those. |
| 523 | */ |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 524 | if (!fmd->fimc[i]->variant->has_cam_if) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 525 | continue; |
| 526 | |
| 527 | flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 528 | |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 529 | sink = &fmd->fimc[i]->vid_cap.subdev.entity; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 530 | ret = media_entity_create_link(source, pad, sink, |
| 531 | FIMC_SD_PAD_SINK, flags); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 532 | if (ret) |
| 533 | return ret; |
| 534 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 535 | /* Notify FIMC capture subdev entity */ |
| 536 | ret = media_entity_call(sink, link_setup, &sink->pads[0], |
| 537 | &source->pads[pad], flags); |
| 538 | if (ret) |
| 539 | break; |
| 540 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 541 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]", |
| 542 | source->name, flags ? '=' : '-', sink->name); |
| 543 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 544 | if (flags == 0 || sensor == NULL) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 545 | continue; |
| 546 | s_info = v4l2_get_subdev_hostdata(sensor); |
| 547 | if (!WARN_ON(s_info == NULL)) { |
| 548 | unsigned long irq_flags; |
| 549 | spin_lock_irqsave(&fmd->slock, irq_flags); |
| 550 | s_info->host = fmd->fimc[i]; |
| 551 | spin_unlock_irqrestore(&fmd->slock, irq_flags); |
| 552 | } |
| 553 | } |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 554 | |
| 555 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { |
| 556 | if (!fmd->fimc_lite[i]) |
| 557 | continue; |
| 558 | |
| 559 | flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0; |
| 560 | |
| 561 | sink = &fmd->fimc_lite[i]->subdev.entity; |
| 562 | ret = media_entity_create_link(source, pad, sink, |
| 563 | FLITE_SD_PAD_SINK, flags); |
| 564 | if (ret) |
| 565 | return ret; |
| 566 | |
| 567 | /* Notify FIMC-LITE subdev entity */ |
| 568 | ret = media_entity_call(sink, link_setup, &sink->pads[0], |
| 569 | &source->pads[pad], flags); |
| 570 | if (ret) |
| 571 | break; |
| 572 | |
| 573 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]", |
| 574 | source->name, flags ? '=' : '-', sink->name); |
| 575 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 579 | /* Create links from FIMC-LITE source pads to other entities */ |
| 580 | static int __fimc_md_create_flite_source_links(struct fimc_md *fmd) |
| 581 | { |
| 582 | struct media_entity *source, *sink; |
| 583 | unsigned int flags = MEDIA_LNK_FL_ENABLED; |
| 584 | int i, ret; |
| 585 | |
| 586 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { |
| 587 | struct fimc_lite *fimc = fmd->fimc_lite[i]; |
| 588 | if (fimc == NULL) |
| 589 | continue; |
| 590 | source = &fimc->subdev.entity; |
| 591 | sink = &fimc->vfd->entity; |
| 592 | /* FIMC-LITE's subdev and video node */ |
| 593 | ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE, |
| 594 | sink, 0, flags); |
| 595 | if (ret) |
| 596 | break; |
| 597 | /* TODO: create links to other entities */ |
| 598 | } |
| 599 | |
| 600 | return ret; |
| 601 | } |
| 602 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 603 | /** |
| 604 | * fimc_md_create_links - create default links between registered entities |
| 605 | * |
| 606 | * Parallel interface sensor entities are connected directly to FIMC capture |
| 607 | * entities. The sensors using MIPI CSIS bus are connected through immutable |
| 608 | * link with CSI receiver entity specified by mux_id. Any registered CSIS |
| 609 | * entity has a link to each registered FIMC capture entity. Enabled links |
| 610 | * are created by default between each subsequent registered sensor and |
| 611 | * subsequent FIMC capture entity. The number of default active links is |
| 612 | * determined by the number of available sensors or FIMC entities, |
| 613 | * whichever is less. |
| 614 | */ |
| 615 | static int fimc_md_create_links(struct fimc_md *fmd) |
| 616 | { |
| 617 | struct v4l2_subdev *sensor, *csis; |
| 618 | struct s5p_fimc_isp_info *pdata; |
| 619 | struct fimc_sensor_info *s_info; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 620 | struct media_entity *source, *sink; |
| 621 | int i, pad, fimc_id = 0; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 622 | int ret = 0; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 623 | u32 flags; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 624 | |
| 625 | for (i = 0; i < fmd->num_sensors; i++) { |
| 626 | if (fmd->sensor[i].subdev == NULL) |
| 627 | continue; |
| 628 | |
| 629 | sensor = fmd->sensor[i].subdev; |
| 630 | s_info = v4l2_get_subdev_hostdata(sensor); |
| 631 | if (!s_info || !s_info->pdata) |
| 632 | continue; |
| 633 | |
| 634 | source = NULL; |
| 635 | pdata = s_info->pdata; |
| 636 | |
| 637 | switch (pdata->bus_type) { |
| 638 | case FIMC_MIPI_CSI2: |
| 639 | if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES, |
| 640 | "Wrong CSI channel id: %d\n", pdata->mux_id)) |
| 641 | return -EINVAL; |
| 642 | |
| 643 | csis = fmd->csis[pdata->mux_id].sd; |
| 644 | if (WARN(csis == NULL, |
| 645 | "MIPI-CSI interface specified " |
| 646 | "but s5p-csis module is not loaded!\n")) |
Sylwester Nawrocki | d12392e | 2011-10-20 05:15:59 -0300 | [diff] [blame] | 647 | return -EINVAL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 648 | |
| 649 | ret = media_entity_create_link(&sensor->entity, 0, |
| 650 | &csis->entity, CSIS_PAD_SINK, |
| 651 | MEDIA_LNK_FL_IMMUTABLE | |
| 652 | MEDIA_LNK_FL_ENABLED); |
| 653 | if (ret) |
| 654 | return ret; |
| 655 | |
| 656 | v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]", |
| 657 | sensor->entity.name, csis->entity.name); |
| 658 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 659 | source = NULL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 660 | break; |
| 661 | |
| 662 | case FIMC_ITU_601...FIMC_ITU_656: |
| 663 | source = &sensor->entity; |
| 664 | pad = 0; |
| 665 | break; |
| 666 | |
| 667 | default: |
| 668 | v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n", |
| 669 | pdata->bus_type); |
| 670 | return -EINVAL; |
| 671 | } |
| 672 | if (source == NULL) |
| 673 | continue; |
| 674 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 675 | ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor, |
| 676 | pad, fimc_id++); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 677 | } |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 678 | |
| 679 | fimc_id = 0; |
| 680 | for (i = 0; i < ARRAY_SIZE(fmd->csis); i++) { |
| 681 | if (fmd->csis[i].sd == NULL) |
| 682 | continue; |
| 683 | source = &fmd->csis[i].sd->entity; |
| 684 | pad = CSIS_PAD_SOURCE; |
| 685 | |
| 686 | ret = __fimc_md_create_fimc_sink_links(fmd, source, NULL, |
| 687 | pad, fimc_id++); |
| 688 | } |
| 689 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 690 | /* Create immutable links between each FIMC's subdev and video node */ |
| 691 | flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED; |
| 692 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 693 | if (!fmd->fimc[i]) |
| 694 | continue; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 695 | source = &fmd->fimc[i]->vid_cap.subdev.entity; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 696 | sink = &fmd->fimc[i]->vid_cap.vfd->entity; |
| 697 | ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE, |
| 698 | sink, 0, flags); |
| 699 | if (ret) |
| 700 | break; |
| 701 | } |
| 702 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 703 | return __fimc_md_create_flite_source_links(fmd); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* |
| 707 | * The peripheral sensor clock management. |
| 708 | */ |
| 709 | static int fimc_md_get_clocks(struct fimc_md *fmd) |
| 710 | { |
| 711 | char clk_name[32]; |
| 712 | struct clk *clock; |
| 713 | int i; |
| 714 | |
| 715 | for (i = 0; i < FIMC_MAX_CAMCLKS; i++) { |
| 716 | snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i); |
| 717 | clock = clk_get(NULL, clk_name); |
| 718 | if (IS_ERR_OR_NULL(clock)) { |
| 719 | v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s", |
| 720 | clk_name); |
| 721 | return -ENXIO; |
| 722 | } |
| 723 | fmd->camclk[i].clock = clock; |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static void fimc_md_put_clocks(struct fimc_md *fmd) |
| 729 | { |
| 730 | int i = FIMC_MAX_CAMCLKS; |
| 731 | |
| 732 | while (--i >= 0) { |
| 733 | if (IS_ERR_OR_NULL(fmd->camclk[i].clock)) |
| 734 | continue; |
| 735 | clk_put(fmd->camclk[i].clock); |
| 736 | fmd->camclk[i].clock = NULL; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | static int __fimc_md_set_camclk(struct fimc_md *fmd, |
| 741 | struct fimc_sensor_info *s_info, |
| 742 | bool on) |
| 743 | { |
| 744 | struct s5p_fimc_isp_info *pdata = s_info->pdata; |
| 745 | struct fimc_camclk_info *camclk; |
| 746 | int ret = 0; |
| 747 | |
| 748 | if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | if (s_info->clk_on == on) |
| 752 | return 0; |
| 753 | camclk = &fmd->camclk[pdata->clk_id]; |
| 754 | |
| 755 | dbg("camclk %d, f: %lu, clk: %p, on: %d", |
| 756 | pdata->clk_id, pdata->clk_frequency, camclk, on); |
| 757 | |
| 758 | if (on) { |
| 759 | if (camclk->use_count > 0 && |
| 760 | camclk->frequency != pdata->clk_frequency) |
| 761 | return -EINVAL; |
| 762 | |
| 763 | if (camclk->use_count++ == 0) { |
| 764 | clk_set_rate(camclk->clock, pdata->clk_frequency); |
| 765 | camclk->frequency = pdata->clk_frequency; |
| 766 | ret = clk_enable(camclk->clock); |
| 767 | } |
| 768 | s_info->clk_on = 1; |
| 769 | dbg("Enabled camclk %d: f: %lu", pdata->clk_id, |
| 770 | clk_get_rate(camclk->clock)); |
| 771 | |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | if (WARN_ON(camclk->use_count == 0)) |
| 776 | return 0; |
| 777 | |
| 778 | if (--camclk->use_count == 0) { |
| 779 | clk_disable(camclk->clock); |
| 780 | s_info->clk_on = 0; |
| 781 | dbg("Disabled camclk %d", pdata->clk_id); |
| 782 | } |
| 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * fimc_md_set_camclk - peripheral sensor clock setup |
| 788 | * @sd: sensor subdev to configure sclk_cam clock for |
| 789 | * @on: 1 to enable or 0 to disable the clock |
| 790 | * |
| 791 | * There are 2 separate clock outputs available in the SoC for external |
| 792 | * image processors. These clocks are shared between all registered FIMC |
| 793 | * devices to which sensors can be attached, either directly or through |
| 794 | * the MIPI CSI receiver. The clock is allowed here to be used by |
| 795 | * multiple sensors concurrently if they use same frequency. |
| 796 | * The per sensor subdev clk_on attribute helps to synchronize accesses |
| 797 | * to the sclk_cam clocks from the video and media device nodes. |
| 798 | * This function should only be called when the graph mutex is held. |
| 799 | */ |
| 800 | int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on) |
| 801 | { |
| 802 | struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd); |
| 803 | struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity); |
| 804 | |
| 805 | return __fimc_md_set_camclk(fmd, s_info, on); |
| 806 | } |
| 807 | |
| 808 | static int fimc_md_link_notify(struct media_pad *source, |
| 809 | struct media_pad *sink, u32 flags) |
| 810 | { |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 811 | struct fimc_lite *fimc_lite = NULL; |
| 812 | struct fimc_dev *fimc = NULL; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 813 | struct fimc_pipeline *pipeline; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 814 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 815 | int ret = 0; |
| 816 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 817 | if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 818 | return 0; |
| 819 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 820 | sd = media_entity_to_v4l2_subdev(sink->entity); |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 821 | |
| 822 | switch (sd->grp_id) { |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 823 | case FLITE_GROUP_ID: |
| 824 | fimc_lite = v4l2_get_subdevdata(sd); |
| 825 | pipeline = &fimc_lite->pipeline; |
| 826 | break; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 827 | case FIMC_GROUP_ID: |
| 828 | fimc = v4l2_get_subdevdata(sd); |
| 829 | pipeline = &fimc->pipeline; |
| 830 | break; |
| 831 | default: |
| 832 | return 0; |
| 833 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 834 | |
| 835 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 836 | ret = __fimc_pipeline_shutdown(pipeline); |
| 837 | pipeline->subdevs[IDX_SENSOR] = NULL; |
| 838 | pipeline->subdevs[IDX_CSIS] = NULL; |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 839 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 840 | if (fimc) { |
| 841 | mutex_lock(&fimc->lock); |
| 842 | fimc_ctrls_delete(fimc->vid_cap.ctx); |
| 843 | mutex_unlock(&fimc->lock); |
| 844 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 845 | return ret; |
| 846 | } |
| 847 | /* |
| 848 | * Link activation. Enable power of pipeline elements only if the |
| 849 | * pipeline is already in use, i.e. its video node is opened. |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 850 | * Recreate the controls destroyed during the link deactivation. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 851 | */ |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 852 | if (fimc) { |
| 853 | mutex_lock(&fimc->lock); |
| 854 | if (fimc->vid_cap.refcnt > 0) { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 855 | ret = __fimc_pipeline_initialize(pipeline, |
| 856 | source->entity, true); |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 857 | if (!ret) |
| 858 | ret = fimc_capture_ctrls_create(fimc); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 859 | } |
| 860 | mutex_unlock(&fimc->lock); |
| 861 | } else { |
| 862 | mutex_lock(&fimc_lite->lock); |
| 863 | if (fimc_lite->ref_count > 0) { |
| 864 | ret = __fimc_pipeline_initialize(pipeline, |
| 865 | source->entity, true); |
| 866 | } |
| 867 | mutex_unlock(&fimc_lite->lock); |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 868 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 869 | return ret ? -EPIPE : ret; |
| 870 | } |
| 871 | |
| 872 | static ssize_t fimc_md_sysfs_show(struct device *dev, |
| 873 | struct device_attribute *attr, char *buf) |
| 874 | { |
| 875 | struct platform_device *pdev = to_platform_device(dev); |
| 876 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 877 | |
| 878 | if (fmd->user_subdev_api) |
| 879 | return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE); |
| 880 | |
| 881 | return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE); |
| 882 | } |
| 883 | |
| 884 | static ssize_t fimc_md_sysfs_store(struct device *dev, |
| 885 | struct device_attribute *attr, |
| 886 | const char *buf, size_t count) |
| 887 | { |
| 888 | struct platform_device *pdev = to_platform_device(dev); |
| 889 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 890 | bool subdev_api; |
| 891 | int i; |
| 892 | |
| 893 | if (!strcmp(buf, "vid-dev\n")) |
| 894 | subdev_api = false; |
| 895 | else if (!strcmp(buf, "sub-dev\n")) |
| 896 | subdev_api = true; |
| 897 | else |
| 898 | return count; |
| 899 | |
| 900 | fmd->user_subdev_api = subdev_api; |
| 901 | for (i = 0; i < FIMC_MAX_DEVS; i++) |
| 902 | if (fmd->fimc[i]) |
| 903 | fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api; |
| 904 | return count; |
| 905 | } |
| 906 | /* |
| 907 | * This device attribute is to select video pipeline configuration method. |
| 908 | * There are following valid values: |
| 909 | * vid-dev - for V4L2 video node API only, subdevice will be configured |
| 910 | * by the host driver. |
| 911 | * sub-dev - for media controller API, subdevs must be configured in user |
| 912 | * space before starting streaming. |
| 913 | */ |
| 914 | static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO, |
| 915 | fimc_md_sysfs_show, fimc_md_sysfs_store); |
| 916 | |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 917 | static int fimc_md_probe(struct platform_device *pdev) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 918 | { |
| 919 | struct v4l2_device *v4l2_dev; |
| 920 | struct fimc_md *fmd; |
| 921 | int ret; |
| 922 | |
Sylwester Nawrocki | 6d91a51 | 2012-01-30 11:37:59 -0300 | [diff] [blame] | 923 | fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 924 | if (!fmd) |
| 925 | return -ENOMEM; |
| 926 | |
| 927 | spin_lock_init(&fmd->slock); |
| 928 | fmd->pdev = pdev; |
| 929 | |
| 930 | strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC", |
| 931 | sizeof(fmd->media_dev.model)); |
| 932 | fmd->media_dev.link_notify = fimc_md_link_notify; |
| 933 | fmd->media_dev.dev = &pdev->dev; |
| 934 | |
| 935 | v4l2_dev = &fmd->v4l2_dev; |
| 936 | v4l2_dev->mdev = &fmd->media_dev; |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 937 | v4l2_dev->notify = fimc_sensor_notify; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 938 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s", |
| 939 | dev_name(&pdev->dev)); |
| 940 | |
| 941 | ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev); |
| 942 | if (ret < 0) { |
| 943 | v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret); |
Sylwester Nawrocki | 6d91a51 | 2012-01-30 11:37:59 -0300 | [diff] [blame] | 944 | return ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 945 | } |
| 946 | ret = media_device_register(&fmd->media_dev); |
| 947 | if (ret < 0) { |
| 948 | v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 949 | goto err_md; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 950 | } |
| 951 | ret = fimc_md_get_clocks(fmd); |
| 952 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 953 | goto err_clk; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 954 | |
| 955 | fmd->user_subdev_api = false; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 956 | |
| 957 | /* Protect the media graph while we're registering entities */ |
| 958 | mutex_lock(&fmd->media_dev.graph_mutex); |
| 959 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 960 | ret = fimc_md_register_platform_entities(fmd); |
| 961 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 962 | goto err_unlock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 963 | |
Sylwester Nawrocki | 5cbf6f16 | 2011-10-07 07:40:00 -0300 | [diff] [blame] | 964 | if (pdev->dev.platform_data) { |
| 965 | ret = fimc_md_register_sensor_entities(fmd); |
| 966 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 967 | goto err_unlock; |
Sylwester Nawrocki | 5cbf6f16 | 2011-10-07 07:40:00 -0300 | [diff] [blame] | 968 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 969 | ret = fimc_md_create_links(fmd); |
| 970 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 971 | goto err_unlock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 972 | ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev); |
| 973 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 974 | goto err_unlock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 975 | |
| 976 | ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 977 | if (ret) |
| 978 | goto err_unlock; |
| 979 | |
| 980 | platform_set_drvdata(pdev, fmd); |
| 981 | mutex_unlock(&fmd->media_dev.graph_mutex); |
| 982 | return 0; |
| 983 | |
| 984 | err_unlock: |
| 985 | mutex_unlock(&fmd->media_dev.graph_mutex); |
| 986 | err_clk: |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 987 | media_device_unregister(&fmd->media_dev); |
| 988 | fimc_md_put_clocks(fmd); |
| 989 | fimc_md_unregister_entities(fmd); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 990 | err_md: |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 991 | v4l2_device_unregister(&fmd->v4l2_dev); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 992 | return ret; |
| 993 | } |
| 994 | |
| 995 | static int __devexit fimc_md_remove(struct platform_device *pdev) |
| 996 | { |
| 997 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 998 | |
| 999 | if (!fmd) |
| 1000 | return 0; |
| 1001 | device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode); |
| 1002 | fimc_md_unregister_entities(fmd); |
| 1003 | media_device_unregister(&fmd->media_dev); |
| 1004 | fimc_md_put_clocks(fmd); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | static struct platform_driver fimc_md_driver = { |
| 1009 | .probe = fimc_md_probe, |
| 1010 | .remove = __devexit_p(fimc_md_remove), |
| 1011 | .driver = { |
| 1012 | .name = "s5p-fimc-md", |
| 1013 | .owner = THIS_MODULE, |
| 1014 | } |
| 1015 | }; |
| 1016 | |
| 1017 | int __init fimc_md_init(void) |
| 1018 | { |
| 1019 | int ret; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 1020 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1021 | request_module("s5p-csis"); |
| 1022 | ret = fimc_register_driver(); |
| 1023 | if (ret) |
| 1024 | return ret; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 1025 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1026 | return platform_driver_register(&fimc_md_driver); |
| 1027 | } |
| 1028 | void __exit fimc_md_exit(void) |
| 1029 | { |
| 1030 | platform_driver_unregister(&fimc_md_driver); |
| 1031 | fimc_unregister_driver(); |
| 1032 | } |
| 1033 | |
| 1034 | module_init(fimc_md_init); |
| 1035 | module_exit(fimc_md_exit); |
| 1036 | |
| 1037 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); |
| 1038 | MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver"); |
| 1039 | MODULE_LICENSE("GPL"); |
| 1040 | MODULE_VERSION("2.0.1"); |