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