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 |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 505 | * @link_mask: bitmask of the fimc devices for which link should be enabled |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 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, |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 510 | int pad, int link_mask) |
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 | |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 527 | flags = ((1 << i) & link_mask) ? 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 | |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 559 | if (link_mask & (1 << (i + FIMC_MAX_DEVS))) |
| 560 | flags = MEDIA_LNK_FL_ENABLED; |
| 561 | else |
| 562 | flags = 0; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 563 | |
| 564 | sink = &fmd->fimc_lite[i]->subdev.entity; |
| 565 | ret = media_entity_create_link(source, pad, sink, |
| 566 | FLITE_SD_PAD_SINK, flags); |
| 567 | if (ret) |
| 568 | return ret; |
| 569 | |
| 570 | /* Notify FIMC-LITE subdev entity */ |
| 571 | ret = media_entity_call(sink, link_setup, &sink->pads[0], |
| 572 | &source->pads[pad], flags); |
| 573 | if (ret) |
| 574 | break; |
| 575 | |
| 576 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]", |
| 577 | source->name, flags ? '=' : '-', sink->name); |
| 578 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 582 | /* Create links from FIMC-LITE source pads to other entities */ |
| 583 | static int __fimc_md_create_flite_source_links(struct fimc_md *fmd) |
| 584 | { |
| 585 | struct media_entity *source, *sink; |
| 586 | unsigned int flags = MEDIA_LNK_FL_ENABLED; |
| 587 | int i, ret; |
| 588 | |
| 589 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { |
| 590 | struct fimc_lite *fimc = fmd->fimc_lite[i]; |
| 591 | if (fimc == NULL) |
| 592 | continue; |
| 593 | source = &fimc->subdev.entity; |
| 594 | sink = &fimc->vfd->entity; |
| 595 | /* FIMC-LITE's subdev and video node */ |
| 596 | ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE, |
| 597 | sink, 0, flags); |
| 598 | if (ret) |
| 599 | break; |
| 600 | /* TODO: create links to other entities */ |
| 601 | } |
| 602 | |
| 603 | return ret; |
| 604 | } |
| 605 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 606 | /** |
| 607 | * fimc_md_create_links - create default links between registered entities |
| 608 | * |
| 609 | * Parallel interface sensor entities are connected directly to FIMC capture |
| 610 | * entities. The sensors using MIPI CSIS bus are connected through immutable |
| 611 | * link with CSI receiver entity specified by mux_id. Any registered CSIS |
| 612 | * entity has a link to each registered FIMC capture entity. Enabled links |
| 613 | * are created by default between each subsequent registered sensor and |
| 614 | * subsequent FIMC capture entity. The number of default active links is |
| 615 | * determined by the number of available sensors or FIMC entities, |
| 616 | * whichever is less. |
| 617 | */ |
| 618 | static int fimc_md_create_links(struct fimc_md *fmd) |
| 619 | { |
| 620 | struct v4l2_subdev *sensor, *csis; |
| 621 | struct s5p_fimc_isp_info *pdata; |
| 622 | struct fimc_sensor_info *s_info; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 623 | struct media_entity *source, *sink; |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 624 | int i, pad, fimc_id = 0, ret = 0; |
| 625 | u32 flags, link_mask = 0; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 626 | |
| 627 | for (i = 0; i < fmd->num_sensors; i++) { |
| 628 | if (fmd->sensor[i].subdev == NULL) |
| 629 | continue; |
| 630 | |
| 631 | sensor = fmd->sensor[i].subdev; |
| 632 | s_info = v4l2_get_subdev_hostdata(sensor); |
| 633 | if (!s_info || !s_info->pdata) |
| 634 | continue; |
| 635 | |
| 636 | source = NULL; |
| 637 | pdata = s_info->pdata; |
| 638 | |
| 639 | switch (pdata->bus_type) { |
| 640 | case FIMC_MIPI_CSI2: |
| 641 | if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES, |
| 642 | "Wrong CSI channel id: %d\n", pdata->mux_id)) |
| 643 | return -EINVAL; |
| 644 | |
| 645 | csis = fmd->csis[pdata->mux_id].sd; |
| 646 | if (WARN(csis == NULL, |
| 647 | "MIPI-CSI interface specified " |
| 648 | "but s5p-csis module is not loaded!\n")) |
Sylwester Nawrocki | d12392e | 2011-10-20 05:15:59 -0300 | [diff] [blame] | 649 | return -EINVAL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 650 | |
| 651 | ret = media_entity_create_link(&sensor->entity, 0, |
| 652 | &csis->entity, CSIS_PAD_SINK, |
| 653 | MEDIA_LNK_FL_IMMUTABLE | |
| 654 | MEDIA_LNK_FL_ENABLED); |
| 655 | if (ret) |
| 656 | return ret; |
| 657 | |
| 658 | v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]", |
| 659 | sensor->entity.name, csis->entity.name); |
| 660 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 661 | source = NULL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 662 | break; |
| 663 | |
| 664 | case FIMC_ITU_601...FIMC_ITU_656: |
| 665 | source = &sensor->entity; |
| 666 | pad = 0; |
| 667 | break; |
| 668 | |
| 669 | default: |
| 670 | v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n", |
| 671 | pdata->bus_type); |
| 672 | return -EINVAL; |
| 673 | } |
| 674 | if (source == NULL) |
| 675 | continue; |
| 676 | |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 677 | link_mask = 1 << fimc_id++; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 678 | ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor, |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 679 | pad, link_mask); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 680 | } |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 681 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 682 | for (i = 0; i < ARRAY_SIZE(fmd->csis); i++) { |
| 683 | if (fmd->csis[i].sd == NULL) |
| 684 | continue; |
| 685 | source = &fmd->csis[i].sd->entity; |
| 686 | pad = CSIS_PAD_SOURCE; |
| 687 | |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 688 | link_mask = 1 << fimc_id++; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 689 | ret = __fimc_md_create_fimc_sink_links(fmd, source, NULL, |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame^] | 690 | pad, link_mask); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 691 | } |
| 692 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 693 | /* Create immutable links between each FIMC's subdev and video node */ |
| 694 | flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED; |
| 695 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 696 | if (!fmd->fimc[i]) |
| 697 | continue; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 698 | source = &fmd->fimc[i]->vid_cap.subdev.entity; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 699 | sink = &fmd->fimc[i]->vid_cap.vfd->entity; |
| 700 | ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE, |
| 701 | sink, 0, flags); |
| 702 | if (ret) |
| 703 | break; |
| 704 | } |
| 705 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 706 | return __fimc_md_create_flite_source_links(fmd); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /* |
| 710 | * The peripheral sensor clock management. |
| 711 | */ |
| 712 | static int fimc_md_get_clocks(struct fimc_md *fmd) |
| 713 | { |
| 714 | char clk_name[32]; |
| 715 | struct clk *clock; |
| 716 | int i; |
| 717 | |
| 718 | for (i = 0; i < FIMC_MAX_CAMCLKS; i++) { |
| 719 | snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i); |
| 720 | clock = clk_get(NULL, clk_name); |
| 721 | if (IS_ERR_OR_NULL(clock)) { |
| 722 | v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s", |
| 723 | clk_name); |
| 724 | return -ENXIO; |
| 725 | } |
| 726 | fmd->camclk[i].clock = clock; |
| 727 | } |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static void fimc_md_put_clocks(struct fimc_md *fmd) |
| 732 | { |
| 733 | int i = FIMC_MAX_CAMCLKS; |
| 734 | |
| 735 | while (--i >= 0) { |
| 736 | if (IS_ERR_OR_NULL(fmd->camclk[i].clock)) |
| 737 | continue; |
| 738 | clk_put(fmd->camclk[i].clock); |
| 739 | fmd->camclk[i].clock = NULL; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | static int __fimc_md_set_camclk(struct fimc_md *fmd, |
| 744 | struct fimc_sensor_info *s_info, |
| 745 | bool on) |
| 746 | { |
| 747 | struct s5p_fimc_isp_info *pdata = s_info->pdata; |
| 748 | struct fimc_camclk_info *camclk; |
| 749 | int ret = 0; |
| 750 | |
| 751 | if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL) |
| 752 | return -EINVAL; |
| 753 | |
| 754 | if (s_info->clk_on == on) |
| 755 | return 0; |
| 756 | camclk = &fmd->camclk[pdata->clk_id]; |
| 757 | |
| 758 | dbg("camclk %d, f: %lu, clk: %p, on: %d", |
| 759 | pdata->clk_id, pdata->clk_frequency, camclk, on); |
| 760 | |
| 761 | if (on) { |
| 762 | if (camclk->use_count > 0 && |
| 763 | camclk->frequency != pdata->clk_frequency) |
| 764 | return -EINVAL; |
| 765 | |
| 766 | if (camclk->use_count++ == 0) { |
| 767 | clk_set_rate(camclk->clock, pdata->clk_frequency); |
| 768 | camclk->frequency = pdata->clk_frequency; |
| 769 | ret = clk_enable(camclk->clock); |
| 770 | } |
| 771 | s_info->clk_on = 1; |
| 772 | dbg("Enabled camclk %d: f: %lu", pdata->clk_id, |
| 773 | clk_get_rate(camclk->clock)); |
| 774 | |
| 775 | return ret; |
| 776 | } |
| 777 | |
| 778 | if (WARN_ON(camclk->use_count == 0)) |
| 779 | return 0; |
| 780 | |
| 781 | if (--camclk->use_count == 0) { |
| 782 | clk_disable(camclk->clock); |
| 783 | s_info->clk_on = 0; |
| 784 | dbg("Disabled camclk %d", pdata->clk_id); |
| 785 | } |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * fimc_md_set_camclk - peripheral sensor clock setup |
| 791 | * @sd: sensor subdev to configure sclk_cam clock for |
| 792 | * @on: 1 to enable or 0 to disable the clock |
| 793 | * |
| 794 | * There are 2 separate clock outputs available in the SoC for external |
| 795 | * image processors. These clocks are shared between all registered FIMC |
| 796 | * devices to which sensors can be attached, either directly or through |
| 797 | * the MIPI CSI receiver. The clock is allowed here to be used by |
| 798 | * multiple sensors concurrently if they use same frequency. |
| 799 | * The per sensor subdev clk_on attribute helps to synchronize accesses |
| 800 | * to the sclk_cam clocks from the video and media device nodes. |
| 801 | * This function should only be called when the graph mutex is held. |
| 802 | */ |
| 803 | int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on) |
| 804 | { |
| 805 | struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd); |
| 806 | struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity); |
| 807 | |
| 808 | return __fimc_md_set_camclk(fmd, s_info, on); |
| 809 | } |
| 810 | |
| 811 | static int fimc_md_link_notify(struct media_pad *source, |
| 812 | struct media_pad *sink, u32 flags) |
| 813 | { |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 814 | struct fimc_lite *fimc_lite = NULL; |
| 815 | struct fimc_dev *fimc = NULL; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 816 | struct fimc_pipeline *pipeline; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 817 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 818 | int ret = 0; |
| 819 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 820 | if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 821 | return 0; |
| 822 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 823 | sd = media_entity_to_v4l2_subdev(sink->entity); |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 824 | |
| 825 | switch (sd->grp_id) { |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 826 | case FLITE_GROUP_ID: |
| 827 | fimc_lite = v4l2_get_subdevdata(sd); |
| 828 | pipeline = &fimc_lite->pipeline; |
| 829 | break; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 830 | case FIMC_GROUP_ID: |
| 831 | fimc = v4l2_get_subdevdata(sd); |
| 832 | pipeline = &fimc->pipeline; |
| 833 | break; |
| 834 | default: |
| 835 | return 0; |
| 836 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 837 | |
| 838 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 839 | ret = __fimc_pipeline_shutdown(pipeline); |
| 840 | pipeline->subdevs[IDX_SENSOR] = NULL; |
| 841 | pipeline->subdevs[IDX_CSIS] = NULL; |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 842 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 843 | if (fimc) { |
| 844 | mutex_lock(&fimc->lock); |
| 845 | fimc_ctrls_delete(fimc->vid_cap.ctx); |
| 846 | mutex_unlock(&fimc->lock); |
| 847 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 848 | return ret; |
| 849 | } |
| 850 | /* |
| 851 | * Link activation. Enable power of pipeline elements only if the |
| 852 | * pipeline is already in use, i.e. its video node is opened. |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 853 | * Recreate the controls destroyed during the link deactivation. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 854 | */ |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 855 | if (fimc) { |
| 856 | mutex_lock(&fimc->lock); |
| 857 | if (fimc->vid_cap.refcnt > 0) { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 858 | ret = __fimc_pipeline_initialize(pipeline, |
| 859 | source->entity, true); |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 860 | if (!ret) |
| 861 | ret = fimc_capture_ctrls_create(fimc); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 862 | } |
| 863 | mutex_unlock(&fimc->lock); |
| 864 | } else { |
| 865 | mutex_lock(&fimc_lite->lock); |
| 866 | if (fimc_lite->ref_count > 0) { |
| 867 | ret = __fimc_pipeline_initialize(pipeline, |
| 868 | source->entity, true); |
| 869 | } |
| 870 | mutex_unlock(&fimc_lite->lock); |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 871 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 872 | return ret ? -EPIPE : ret; |
| 873 | } |
| 874 | |
| 875 | static ssize_t fimc_md_sysfs_show(struct device *dev, |
| 876 | struct device_attribute *attr, char *buf) |
| 877 | { |
| 878 | struct platform_device *pdev = to_platform_device(dev); |
| 879 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 880 | |
| 881 | if (fmd->user_subdev_api) |
| 882 | return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE); |
| 883 | |
| 884 | return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE); |
| 885 | } |
| 886 | |
| 887 | static ssize_t fimc_md_sysfs_store(struct device *dev, |
| 888 | struct device_attribute *attr, |
| 889 | const char *buf, size_t count) |
| 890 | { |
| 891 | struct platform_device *pdev = to_platform_device(dev); |
| 892 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 893 | bool subdev_api; |
| 894 | int i; |
| 895 | |
| 896 | if (!strcmp(buf, "vid-dev\n")) |
| 897 | subdev_api = false; |
| 898 | else if (!strcmp(buf, "sub-dev\n")) |
| 899 | subdev_api = true; |
| 900 | else |
| 901 | return count; |
| 902 | |
| 903 | fmd->user_subdev_api = subdev_api; |
| 904 | for (i = 0; i < FIMC_MAX_DEVS; i++) |
| 905 | if (fmd->fimc[i]) |
| 906 | fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api; |
| 907 | return count; |
| 908 | } |
| 909 | /* |
| 910 | * This device attribute is to select video pipeline configuration method. |
| 911 | * There are following valid values: |
| 912 | * vid-dev - for V4L2 video node API only, subdevice will be configured |
| 913 | * by the host driver. |
| 914 | * sub-dev - for media controller API, subdevs must be configured in user |
| 915 | * space before starting streaming. |
| 916 | */ |
| 917 | static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO, |
| 918 | fimc_md_sysfs_show, fimc_md_sysfs_store); |
| 919 | |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 920 | static int fimc_md_probe(struct platform_device *pdev) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 921 | { |
| 922 | struct v4l2_device *v4l2_dev; |
| 923 | struct fimc_md *fmd; |
| 924 | int ret; |
| 925 | |
Sylwester Nawrocki | 6d91a51 | 2012-01-30 11:37:59 -0300 | [diff] [blame] | 926 | fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 927 | if (!fmd) |
| 928 | return -ENOMEM; |
| 929 | |
| 930 | spin_lock_init(&fmd->slock); |
| 931 | fmd->pdev = pdev; |
| 932 | |
| 933 | strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC", |
| 934 | sizeof(fmd->media_dev.model)); |
| 935 | fmd->media_dev.link_notify = fimc_md_link_notify; |
| 936 | fmd->media_dev.dev = &pdev->dev; |
| 937 | |
| 938 | v4l2_dev = &fmd->v4l2_dev; |
| 939 | v4l2_dev->mdev = &fmd->media_dev; |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 940 | v4l2_dev->notify = fimc_sensor_notify; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 941 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s", |
| 942 | dev_name(&pdev->dev)); |
| 943 | |
| 944 | ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev); |
| 945 | if (ret < 0) { |
| 946 | v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret); |
Sylwester Nawrocki | 6d91a51 | 2012-01-30 11:37:59 -0300 | [diff] [blame] | 947 | return ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 948 | } |
| 949 | ret = media_device_register(&fmd->media_dev); |
| 950 | if (ret < 0) { |
| 951 | v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 952 | goto err_md; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 953 | } |
| 954 | ret = fimc_md_get_clocks(fmd); |
| 955 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 956 | goto err_clk; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 957 | |
| 958 | fmd->user_subdev_api = false; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 959 | |
| 960 | /* Protect the media graph while we're registering entities */ |
| 961 | mutex_lock(&fmd->media_dev.graph_mutex); |
| 962 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 963 | ret = fimc_md_register_platform_entities(fmd); |
| 964 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 965 | goto err_unlock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 966 | |
Sylwester Nawrocki | 5cbf6f16 | 2011-10-07 07:40:00 -0300 | [diff] [blame] | 967 | if (pdev->dev.platform_data) { |
| 968 | ret = fimc_md_register_sensor_entities(fmd); |
| 969 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 970 | goto err_unlock; |
Sylwester Nawrocki | 5cbf6f16 | 2011-10-07 07:40:00 -0300 | [diff] [blame] | 971 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 972 | ret = fimc_md_create_links(fmd); |
| 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 | ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev); |
| 976 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 977 | goto err_unlock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 978 | |
| 979 | ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 980 | if (ret) |
| 981 | goto err_unlock; |
| 982 | |
| 983 | platform_set_drvdata(pdev, fmd); |
| 984 | mutex_unlock(&fmd->media_dev.graph_mutex); |
| 985 | return 0; |
| 986 | |
| 987 | err_unlock: |
| 988 | mutex_unlock(&fmd->media_dev.graph_mutex); |
| 989 | err_clk: |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 990 | media_device_unregister(&fmd->media_dev); |
| 991 | fimc_md_put_clocks(fmd); |
| 992 | fimc_md_unregister_entities(fmd); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 993 | err_md: |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 994 | v4l2_device_unregister(&fmd->v4l2_dev); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | static int __devexit fimc_md_remove(struct platform_device *pdev) |
| 999 | { |
| 1000 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 1001 | |
| 1002 | if (!fmd) |
| 1003 | return 0; |
| 1004 | device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode); |
| 1005 | fimc_md_unregister_entities(fmd); |
| 1006 | media_device_unregister(&fmd->media_dev); |
| 1007 | fimc_md_put_clocks(fmd); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | static struct platform_driver fimc_md_driver = { |
| 1012 | .probe = fimc_md_probe, |
| 1013 | .remove = __devexit_p(fimc_md_remove), |
| 1014 | .driver = { |
| 1015 | .name = "s5p-fimc-md", |
| 1016 | .owner = THIS_MODULE, |
| 1017 | } |
| 1018 | }; |
| 1019 | |
| 1020 | int __init fimc_md_init(void) |
| 1021 | { |
| 1022 | int ret; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 1023 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1024 | request_module("s5p-csis"); |
| 1025 | ret = fimc_register_driver(); |
| 1026 | if (ret) |
| 1027 | return ret; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 1028 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1029 | return platform_driver_register(&fimc_md_driver); |
| 1030 | } |
| 1031 | void __exit fimc_md_exit(void) |
| 1032 | { |
| 1033 | platform_driver_unregister(&fimc_md_driver); |
| 1034 | fimc_unregister_driver(); |
| 1035 | } |
| 1036 | |
| 1037 | module_init(fimc_md_init); |
| 1038 | module_exit(fimc_md_exit); |
| 1039 | |
| 1040 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); |
| 1041 | MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver"); |
| 1042 | MODULE_LICENSE("GPL"); |
| 1043 | MODULE_VERSION("2.0.1"); |