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 | * |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 4 | * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. |
| 5 | * Sylwester Nawrocki <s.nawrocki@samsung.com> |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 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> |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 20 | #include <linux/of.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/of_i2c.h> |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/pm_runtime.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/slab.h> |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 28 | #include <media/v4l2-ctrls.h> |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 29 | #include <media/v4l2-of.h> |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 30 | #include <media/media-device.h> |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 31 | #include <media/s5p_fimc.h> |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 32 | |
| 33 | #include "fimc-core.h" |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 34 | #include "fimc-lite.h" |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 35 | #include "fimc-mdevice.h" |
| 36 | #include "mipi-csis.h" |
| 37 | |
| 38 | static int __fimc_md_set_camclk(struct fimc_md *fmd, |
| 39 | struct fimc_sensor_info *s_info, |
| 40 | bool on); |
| 41 | /** |
| 42 | * fimc_pipeline_prepare - update pipeline information with subdevice pointers |
Sylwester Nawrocki | 39bb6df6 | 2013-03-08 08:58:34 -0300 | [diff] [blame] | 43 | * @me: media entity terminating the pipeline |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 44 | * |
| 45 | * Caller holds the graph mutex. |
| 46 | */ |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 47 | static void fimc_pipeline_prepare(struct fimc_pipeline *p, |
| 48 | struct media_entity *me) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 49 | { |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 50 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 51 | int i; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 52 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 53 | for (i = 0; i < IDX_MAX; i++) |
| 54 | p->subdevs[i] = NULL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 55 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 56 | while (1) { |
Sylwester Nawrocki | 39bb6df6 | 2013-03-08 08:58:34 -0300 | [diff] [blame] | 57 | struct media_pad *pad = NULL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 58 | |
Sylwester Nawrocki | 39bb6df6 | 2013-03-08 08:58:34 -0300 | [diff] [blame] | 59 | /* Find remote source pad */ |
| 60 | for (i = 0; i < me->num_pads; i++) { |
| 61 | struct media_pad *spad = &me->pads[i]; |
| 62 | if (!(spad->flags & MEDIA_PAD_FL_SINK)) |
| 63 | continue; |
| 64 | pad = media_entity_remote_source(spad); |
| 65 | if (pad) |
| 66 | break; |
| 67 | } |
| 68 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 69 | if (pad == NULL || |
| 70 | media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
| 71 | break; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 72 | sd = media_entity_to_v4l2_subdev(pad->entity); |
| 73 | |
| 74 | switch (sd->grp_id) { |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 75 | case GRP_ID_FIMC_IS_SENSOR: |
| 76 | case GRP_ID_SENSOR: |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 77 | p->subdevs[IDX_SENSOR] = sd; |
| 78 | break; |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 79 | case GRP_ID_CSIS: |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 80 | p->subdevs[IDX_CSIS] = sd; |
| 81 | break; |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 82 | case GRP_ID_FLITE: |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 83 | p->subdevs[IDX_FLITE] = sd; |
| 84 | break; |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 85 | case GRP_ID_FIMC: |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 86 | /* No need to control FIMC subdev through subdev ops */ |
| 87 | break; |
| 88 | default: |
| 89 | pr_warn("%s: Unknown subdev grp_id: %#x\n", |
| 90 | __func__, sd->grp_id); |
| 91 | } |
Sylwester Nawrocki | 39bb6df6 | 2013-03-08 08:58:34 -0300 | [diff] [blame] | 92 | me = &sd->entity; |
| 93 | if (me->num_pads == 1) |
| 94 | break; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * __subdev_set_power - change power state of a single subdev |
| 100 | * @sd: subdevice to change power state for |
| 101 | * @on: 1 to enable power or 0 to disable |
| 102 | * |
| 103 | * Return result of s_power subdev operation or -ENXIO if sd argument |
| 104 | * is NULL. Return 0 if the subdevice does not implement s_power. |
| 105 | */ |
| 106 | static int __subdev_set_power(struct v4l2_subdev *sd, int on) |
| 107 | { |
| 108 | int *use_count; |
| 109 | int ret; |
| 110 | |
| 111 | if (sd == NULL) |
| 112 | return -ENXIO; |
| 113 | |
| 114 | use_count = &sd->entity.use_count; |
| 115 | if (on && (*use_count)++ > 0) |
| 116 | return 0; |
| 117 | else if (!on && (*use_count == 0 || --(*use_count) > 0)) |
| 118 | return 0; |
| 119 | ret = v4l2_subdev_call(sd, core, s_power, on); |
| 120 | |
| 121 | return ret != -ENOIOCTLCMD ? ret : 0; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * fimc_pipeline_s_power - change power state of all pipeline subdevs |
| 126 | * @fimc: fimc device terminating the pipeline |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 127 | * @state: true to power on, false to power off |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 128 | * |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 129 | * Needs to be called with the graph mutex held. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 130 | */ |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 131 | static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 132 | { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 133 | unsigned int i; |
| 134 | int ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 135 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 136 | if (p->subdevs[IDX_SENSOR] == NULL) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 137 | return -ENXIO; |
| 138 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 139 | for (i = 0; i < IDX_MAX; i++) { |
| 140 | unsigned int idx = state ? (IDX_MAX - 1) - i : i; |
| 141 | |
| 142 | ret = __subdev_set_power(p->subdevs[idx], state); |
| 143 | if (ret < 0 && ret != -ENXIO) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 144 | return ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 145 | } |
| 146 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 147 | return 0; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 151 | * __fimc_pipeline_open - update the pipeline information, enable power |
| 152 | * of all pipeline subdevs and the sensor clock |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 153 | * @me: media entity to start graph walk with |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 154 | * @prepare: true to walk the current pipeline and acquire all subdevs |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 155 | * |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 156 | * Called with the graph mutex held. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 157 | */ |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 158 | static int __fimc_pipeline_open(struct fimc_pipeline *p, |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 159 | struct media_entity *me, bool prepare) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 160 | { |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 161 | struct fimc_md *fmd = entity_to_fimc_mdev(me); |
| 162 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 163 | int ret; |
| 164 | |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 165 | if (WARN_ON(p == NULL || me == NULL)) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 166 | return -EINVAL; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 167 | |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 168 | if (prepare) |
| 169 | fimc_pipeline_prepare(p, me); |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 170 | |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 171 | sd = p->subdevs[IDX_SENSOR]; |
| 172 | if (sd == NULL) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */ |
| 176 | if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) { |
| 177 | ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]); |
| 178 | if (ret < 0) |
| 179 | return ret; |
| 180 | } |
| 181 | ret = fimc_md_set_camclk(sd, true); |
| 182 | if (ret < 0) |
| 183 | goto err_wbclk; |
| 184 | |
| 185 | ret = fimc_pipeline_s_power(p, 1); |
| 186 | if (!ret) |
| 187 | return 0; |
| 188 | |
| 189 | fimc_md_set_camclk(sd, false); |
| 190 | |
| 191 | err_wbclk: |
| 192 | if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) |
| 193 | clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]); |
| 194 | |
| 195 | return ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 196 | } |
| 197 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 198 | /** |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 199 | * __fimc_pipeline_close - disable the sensor clock and pipeline power |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 200 | * @fimc: fimc device terminating the pipeline |
| 201 | * |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 202 | * Disable power of all subdevs and turn the external sensor clock off. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 203 | */ |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 204 | static int __fimc_pipeline_close(struct fimc_pipeline *p) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 205 | { |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 206 | struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL; |
| 207 | struct fimc_md *fmd; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 208 | int ret = 0; |
| 209 | |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 210 | if (WARN_ON(sd == NULL)) |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 213 | if (p->subdevs[IDX_SENSOR]) { |
| 214 | ret = fimc_pipeline_s_power(p, 0); |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 215 | fimc_md_set_camclk(sd, false); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 216 | } |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 217 | |
| 218 | fmd = entity_to_fimc_mdev(&sd->entity); |
| 219 | |
| 220 | /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */ |
| 221 | if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) |
| 222 | clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]); |
| 223 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 224 | return ret == -ENXIO ? 0 : ret; |
| 225 | } |
| 226 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 227 | /** |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 228 | * __fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 229 | * @pipeline: video pipeline structure |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 230 | * @on: passed as the s_stream call argument |
| 231 | */ |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 232 | static int __fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 233 | { |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 234 | int i, ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 235 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 236 | if (p->subdevs[IDX_SENSOR] == NULL) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 237 | return -ENODEV; |
| 238 | |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 239 | for (i = 0; i < IDX_MAX; i++) { |
| 240 | unsigned int idx = on ? (IDX_MAX - 1) - i : i; |
| 241 | |
| 242 | ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on); |
| 243 | |
| 244 | if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 250 | } |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 251 | |
| 252 | /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */ |
| 253 | static const struct fimc_pipeline_ops fimc_pipeline_ops = { |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 254 | .open = __fimc_pipeline_open, |
| 255 | .close = __fimc_pipeline_close, |
| 256 | .set_stream = __fimc_pipeline_s_stream, |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 257 | }; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * Sensor subdevice helper functions |
| 261 | */ |
| 262 | static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd, |
| 263 | struct fimc_sensor_info *s_info) |
| 264 | { |
| 265 | struct i2c_adapter *adapter; |
| 266 | struct v4l2_subdev *sd = NULL; |
| 267 | |
| 268 | if (!s_info || !fmd) |
| 269 | return NULL; |
| 270 | |
Sylwester Nawrocki | 6612a08 | 2012-09-17 06:00:48 -0300 | [diff] [blame] | 271 | adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 272 | if (!adapter) { |
| 273 | v4l2_warn(&fmd->v4l2_dev, |
| 274 | "Failed to get I2C adapter %d, deferring probe\n", |
Sylwester Nawrocki | 6612a08 | 2012-09-17 06:00:48 -0300 | [diff] [blame] | 275 | s_info->pdata.i2c_bus_num); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 276 | return ERR_PTR(-EPROBE_DEFER); |
| 277 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 278 | sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter, |
Sylwester Nawrocki | 6612a08 | 2012-09-17 06:00:48 -0300 | [diff] [blame] | 279 | s_info->pdata.board_info, NULL); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 280 | if (IS_ERR_OR_NULL(sd)) { |
Sylwester Nawrocki | 7acde02 | 2011-09-13 14:07:09 -0300 | [diff] [blame] | 281 | i2c_put_adapter(adapter); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 282 | v4l2_warn(&fmd->v4l2_dev, |
| 283 | "Failed to acquire subdev %s, deferring probe\n", |
Sylwester Nawrocki | 6612a08 | 2012-09-17 06:00:48 -0300 | [diff] [blame] | 284 | s_info->pdata.board_info->type); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 285 | return ERR_PTR(-EPROBE_DEFER); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 286 | } |
| 287 | v4l2_set_subdev_hostdata(sd, s_info); |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 288 | sd->grp_id = GRP_ID_SENSOR; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 289 | |
| 290 | v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n", |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 291 | sd->name); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 292 | return sd; |
| 293 | } |
| 294 | |
| 295 | static void fimc_md_unregister_sensor(struct v4l2_subdev *sd) |
| 296 | { |
| 297 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Sylwester Nawrocki | 7acde02 | 2011-09-13 14:07:09 -0300 | [diff] [blame] | 298 | struct i2c_adapter *adapter; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 299 | |
| 300 | if (!client) |
| 301 | return; |
| 302 | v4l2_device_unregister_subdev(sd); |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 303 | |
| 304 | if (!client->dev.of_node) { |
| 305 | adapter = client->adapter; |
| 306 | i2c_unregister_device(client); |
| 307 | if (adapter) |
| 308 | i2c_put_adapter(adapter); |
| 309 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 310 | } |
| 311 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 312 | #ifdef CONFIG_OF |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 313 | /* Register I2C client subdev associated with @node. */ |
| 314 | static int fimc_md_of_add_sensor(struct fimc_md *fmd, |
| 315 | struct device_node *node, int index) |
| 316 | { |
| 317 | struct fimc_sensor_info *si; |
| 318 | struct i2c_client *client; |
| 319 | struct v4l2_subdev *sd; |
| 320 | int ret; |
| 321 | |
| 322 | if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) |
| 323 | return -EINVAL; |
| 324 | si = &fmd->sensor[index]; |
| 325 | |
| 326 | client = of_find_i2c_device_by_node(node); |
| 327 | if (!client) |
| 328 | return -EPROBE_DEFER; |
| 329 | |
| 330 | device_lock(&client->dev); |
| 331 | |
| 332 | if (!client->driver || |
| 333 | !try_module_get(client->driver->driver.owner)) { |
| 334 | ret = -EPROBE_DEFER; |
| 335 | v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n", |
| 336 | node->full_name); |
| 337 | goto dev_put; |
| 338 | } |
| 339 | |
| 340 | /* Enable sensor's master clock */ |
| 341 | ret = __fimc_md_set_camclk(fmd, si, true); |
| 342 | if (ret < 0) |
| 343 | goto mod_put; |
| 344 | sd = i2c_get_clientdata(client); |
| 345 | |
| 346 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
| 347 | __fimc_md_set_camclk(fmd, si, false); |
| 348 | if (ret < 0) |
| 349 | goto mod_put; |
| 350 | |
| 351 | v4l2_set_subdev_hostdata(sd, si); |
| 352 | sd->grp_id = GRP_ID_SENSOR; |
| 353 | si->subdev = sd; |
| 354 | v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n", |
| 355 | sd->name, fmd->num_sensors); |
| 356 | fmd->num_sensors++; |
| 357 | |
| 358 | mod_put: |
| 359 | module_put(client->driver->driver.owner); |
| 360 | dev_put: |
| 361 | device_unlock(&client->dev); |
| 362 | put_device(&client->dev); |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | /* Parse port node and register as a sub-device any sensor specified there. */ |
| 367 | static int fimc_md_parse_port_node(struct fimc_md *fmd, |
| 368 | struct device_node *port, |
| 369 | unsigned int index) |
| 370 | { |
| 371 | struct device_node *rem, *ep, *np; |
| 372 | struct fimc_source_info *pd; |
| 373 | struct v4l2_of_endpoint endpoint; |
| 374 | int ret; |
| 375 | u32 val; |
| 376 | |
| 377 | pd = &fmd->sensor[index].pdata; |
| 378 | |
| 379 | /* Assume here a port node can have only one endpoint node. */ |
| 380 | ep = of_get_next_child(port, NULL); |
| 381 | if (!ep) |
| 382 | return 0; |
| 383 | |
| 384 | v4l2_of_parse_endpoint(ep, &endpoint); |
| 385 | if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS) |
| 386 | return -EINVAL; |
| 387 | |
| 388 | pd->mux_id = (endpoint.port - 1) & 0x1; |
| 389 | |
| 390 | rem = v4l2_of_get_remote_port_parent(ep); |
| 391 | of_node_put(ep); |
| 392 | if (rem == NULL) { |
| 393 | v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n", |
| 394 | ep->full_name); |
| 395 | return 0; |
| 396 | } |
| 397 | if (!of_property_read_u32(rem, "samsung,camclk-out", &val)) |
| 398 | pd->clk_id = val; |
| 399 | |
| 400 | if (!of_property_read_u32(rem, "clock-frequency", &val)) |
| 401 | pd->clk_frequency = val; |
| 402 | |
| 403 | if (pd->clk_frequency == 0) { |
| 404 | v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n", |
| 405 | rem->full_name); |
| 406 | of_node_put(rem); |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | |
| 410 | if (fimc_input_is_parallel(endpoint.port)) { |
| 411 | if (endpoint.bus_type == V4L2_MBUS_PARALLEL) |
| 412 | pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601; |
| 413 | else |
| 414 | pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656; |
| 415 | pd->flags = endpoint.bus.parallel.flags; |
| 416 | } else if (fimc_input_is_mipi_csi(endpoint.port)) { |
| 417 | /* |
| 418 | * MIPI CSI-2: only input mux selection and |
| 419 | * the sensor's clock frequency is needed. |
| 420 | */ |
| 421 | pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2; |
| 422 | } else { |
| 423 | v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n", |
| 424 | endpoint.port, rem->full_name); |
| 425 | } |
| 426 | /* |
| 427 | * For FIMC-IS handled sensors, that are placed under i2c-isp device |
| 428 | * node, FIMC is connected to the FIMC-IS through its ISP Writeback |
| 429 | * input. Sensors are attached to the FIMC-LITE hostdata interface |
| 430 | * directly or through MIPI-CSIS, depending on the external media bus |
| 431 | * used. This needs to be handled in a more reliable way, not by just |
| 432 | * checking parent's node name. |
| 433 | */ |
| 434 | np = of_get_parent(rem); |
| 435 | |
| 436 | if (np && !of_node_cmp(np->name, "i2c-isp")) |
| 437 | pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK; |
| 438 | else |
| 439 | pd->fimc_bus_type = pd->sensor_bus_type; |
| 440 | |
| 441 | ret = fimc_md_of_add_sensor(fmd, rem, index); |
| 442 | of_node_put(rem); |
| 443 | |
| 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | /* Register all SoC external sub-devices */ |
| 448 | static int fimc_md_of_sensors_register(struct fimc_md *fmd, |
| 449 | struct device_node *np) |
| 450 | { |
| 451 | struct device_node *parent = fmd->pdev->dev.of_node; |
| 452 | struct device_node *node, *ports; |
| 453 | int index = 0; |
| 454 | int ret; |
| 455 | |
| 456 | /* Attach sensors linked to MIPI CSI-2 receivers */ |
| 457 | for_each_available_child_of_node(parent, node) { |
| 458 | struct device_node *port; |
| 459 | |
| 460 | if (of_node_cmp(node->name, "csis")) |
| 461 | continue; |
| 462 | /* The csis node can have only port subnode. */ |
| 463 | port = of_get_next_child(node, NULL); |
| 464 | if (!port) |
| 465 | continue; |
| 466 | |
| 467 | ret = fimc_md_parse_port_node(fmd, port, index); |
| 468 | if (ret < 0) |
| 469 | return ret; |
| 470 | index++; |
| 471 | } |
| 472 | |
| 473 | /* Attach sensors listed in the parallel-ports node */ |
| 474 | ports = of_get_child_by_name(parent, "parallel-ports"); |
| 475 | if (!ports) |
| 476 | return 0; |
| 477 | |
| 478 | for_each_child_of_node(ports, node) { |
| 479 | ret = fimc_md_parse_port_node(fmd, node, index); |
| 480 | if (ret < 0) |
| 481 | break; |
| 482 | index++; |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 488 | static int __of_get_csis_id(struct device_node *np) |
| 489 | { |
| 490 | u32 reg = 0; |
| 491 | |
| 492 | np = of_get_child_by_name(np, "port"); |
| 493 | if (!np) |
| 494 | return -EINVAL; |
| 495 | of_property_read_u32(np, "reg", ®); |
| 496 | return reg - FIMC_INPUT_MIPI_CSI2_0; |
| 497 | } |
| 498 | #else |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 499 | #define fimc_md_of_sensors_register(fmd, np) (-ENOSYS) |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 500 | #define __of_get_csis_id(np) (-ENOSYS) |
| 501 | #endif |
| 502 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 503 | static int fimc_md_register_sensor_entities(struct fimc_md *fmd) |
| 504 | { |
| 505 | struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data; |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 506 | struct device_node *of_node = fmd->pdev->dev.of_node; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 507 | struct fimc_dev *fd = NULL; |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 508 | int num_clients = 0; |
| 509 | int ret, i; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * Runtime resume one of the FIMC entities to make sure |
| 513 | * the sclk_cam clocks are not globally disabled. |
| 514 | */ |
| 515 | for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++) |
| 516 | if (fmd->fimc[i]) |
| 517 | fd = fmd->fimc[i]; |
| 518 | if (!fd) |
| 519 | return -ENXIO; |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 520 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 521 | ret = pm_runtime_get_sync(&fd->pdev->dev); |
| 522 | if (ret < 0) |
| 523 | return ret; |
| 524 | |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 525 | if (of_node) { |
| 526 | fmd->num_sensors = 0; |
| 527 | ret = fimc_md_of_sensors_register(fmd, of_node); |
| 528 | } else if (pdata) { |
| 529 | WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor)); |
| 530 | num_clients = min_t(u32, pdata->num_clients, |
| 531 | ARRAY_SIZE(fmd->sensor)); |
| 532 | fmd->num_sensors = num_clients; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 533 | |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 534 | for (i = 0; i < num_clients; i++) { |
| 535 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 536 | |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 537 | fmd->sensor[i].pdata = pdata->source_info[i]; |
| 538 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true); |
| 539 | if (ret) |
| 540 | break; |
| 541 | sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]); |
| 542 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 543 | |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 544 | if (IS_ERR(sd)) { |
| 545 | fmd->sensor[i].subdev = NULL; |
| 546 | ret = PTR_ERR(sd); |
| 547 | break; |
| 548 | } |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 549 | fmd->sensor[i].subdev = sd; |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 550 | if (ret) |
| 551 | break; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 552 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 553 | } |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 554 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 555 | pm_runtime_put(&fd->pdev->dev); |
| 556 | return ret; |
| 557 | } |
| 558 | |
| 559 | /* |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 560 | * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 561 | */ |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 562 | |
| 563 | static int register_fimc_lite_entity(struct fimc_md *fmd, |
| 564 | struct fimc_lite *fimc_lite) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 565 | { |
Sachin Kamat | 8163ec0 | 2012-10-25 10:56:00 -0300 | [diff] [blame] | 566 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | afd7348 | 2012-10-25 10:48:44 -0300 | [diff] [blame] | 567 | int ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 568 | |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 569 | if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS || |
| 570 | fmd->fimc_lite[fimc_lite->index])) |
| 571 | return -EBUSY; |
| 572 | |
| 573 | sd = &fimc_lite->subdev; |
| 574 | sd->grp_id = GRP_ID_FLITE; |
| 575 | v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops); |
| 576 | |
| 577 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
| 578 | if (!ret) |
| 579 | fmd->fimc_lite[fimc_lite->index] = fimc_lite; |
| 580 | else |
| 581 | v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n", |
| 582 | fimc_lite->index); |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc) |
| 587 | { |
| 588 | struct v4l2_subdev *sd; |
| 589 | int ret; |
| 590 | |
| 591 | if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id])) |
| 592 | return -EBUSY; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 593 | |
Sachin Kamat | 8163ec0 | 2012-10-25 10:56:00 -0300 | [diff] [blame] | 594 | sd = &fimc->vid_cap.subdev; |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 595 | sd->grp_id = GRP_ID_FIMC; |
Sylwester Nawrocki | 97d66c4 | 2012-11-22 06:17:19 -0300 | [diff] [blame] | 596 | v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 597 | |
| 598 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 599 | if (!ret) { |
| 600 | fmd->fimc[fimc->id] = fimc; |
| 601 | fimc->vid_cap.user_subdev_api = fmd->user_subdev_api; |
| 602 | } else { |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 603 | v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n", |
| 604 | fimc->id, ret); |
| 605 | } |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 606 | return ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 607 | } |
| 608 | |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 609 | static int register_csis_entity(struct fimc_md *fmd, |
| 610 | struct platform_device *pdev, |
| 611 | struct v4l2_subdev *sd) |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 612 | { |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 613 | struct device_node *node = pdev->dev.of_node; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 614 | int id, ret; |
| 615 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 616 | id = node ? __of_get_csis_id(node) : max(0, pdev->id); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 617 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 618 | if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES)) |
| 619 | return -ENOENT; |
| 620 | |
| 621 | if (WARN_ON(fmd->csis[id].sd)) |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 622 | return -EBUSY; |
| 623 | |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 624 | sd->grp_id = GRP_ID_CSIS; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 625 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
Sylwester Nawrocki | afd7348 | 2012-10-25 10:48:44 -0300 | [diff] [blame] | 626 | if (!ret) |
| 627 | fmd->csis[id].sd = sd; |
| 628 | else |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 629 | v4l2_err(&fmd->v4l2_dev, |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 630 | "Failed to register MIPI-CSIS.%d (%d)\n", id, ret); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 631 | return ret; |
| 632 | } |
| 633 | |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 634 | static int fimc_md_register_platform_entity(struct fimc_md *fmd, |
| 635 | struct platform_device *pdev, |
| 636 | int plat_entity) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 637 | { |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 638 | struct device *dev = &pdev->dev; |
| 639 | int ret = -EPROBE_DEFER; |
| 640 | void *drvdata; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 641 | |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 642 | /* Lock to ensure dev->driver won't change. */ |
| 643 | device_lock(dev); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 644 | |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 645 | if (!dev->driver || !try_module_get(dev->driver->owner)) |
| 646 | goto dev_unlock; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 647 | |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 648 | drvdata = dev_get_drvdata(dev); |
| 649 | /* Some subdev didn't probe succesfully id drvdata is NULL */ |
| 650 | if (drvdata) { |
| 651 | switch (plat_entity) { |
| 652 | case IDX_FIMC: |
| 653 | ret = register_fimc_entity(fmd, drvdata); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 654 | break; |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 655 | case IDX_FLITE: |
| 656 | ret = register_fimc_lite_entity(fmd, drvdata); |
| 657 | break; |
| 658 | case IDX_CSIS: |
| 659 | ret = register_csis_entity(fmd, pdev, drvdata); |
| 660 | break; |
| 661 | default: |
| 662 | ret = -ENODEV; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 663 | } |
| 664 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 665 | |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 666 | module_put(dev->driver->owner); |
| 667 | dev_unlock: |
| 668 | device_unlock(dev); |
| 669 | if (ret == -EPROBE_DEFER) |
| 670 | dev_info(&fmd->pdev->dev, "deferring %s device registration\n", |
| 671 | dev_name(dev)); |
| 672 | else if (ret < 0) |
| 673 | dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n", |
| 674 | dev_name(dev), ret); |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | static int fimc_md_pdev_match(struct device *dev, void *data) |
| 679 | { |
| 680 | struct platform_device *pdev = to_platform_device(dev); |
| 681 | int plat_entity = -1; |
| 682 | int ret; |
| 683 | char *p; |
| 684 | |
| 685 | if (!get_device(dev)) |
| 686 | return -ENODEV; |
| 687 | |
| 688 | if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) { |
| 689 | plat_entity = IDX_CSIS; |
| 690 | } else if (!strcmp(pdev->name, FIMC_LITE_DRV_NAME)) { |
| 691 | plat_entity = IDX_FLITE; |
| 692 | } else { |
| 693 | p = strstr(pdev->name, "fimc"); |
| 694 | if (p && *(p + 4) == 0) |
| 695 | plat_entity = IDX_FIMC; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 696 | } |
| 697 | |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 698 | if (plat_entity >= 0) |
| 699 | ret = fimc_md_register_platform_entity(data, pdev, |
| 700 | plat_entity); |
| 701 | put_device(dev); |
| 702 | return 0; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 703 | } |
| 704 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 705 | /* Register FIMC, FIMC-LITE and CSIS media entities */ |
| 706 | #ifdef CONFIG_OF |
| 707 | static int fimc_md_register_of_platform_entities(struct fimc_md *fmd, |
| 708 | struct device_node *parent) |
| 709 | { |
| 710 | struct device_node *node; |
| 711 | int ret = 0; |
| 712 | |
| 713 | for_each_available_child_of_node(parent, node) { |
| 714 | struct platform_device *pdev; |
| 715 | int plat_entity = -1; |
| 716 | |
| 717 | pdev = of_find_device_by_node(node); |
| 718 | if (!pdev) |
| 719 | continue; |
| 720 | |
| 721 | /* If driver of any entity isn't ready try all again later. */ |
| 722 | if (!strcmp(node->name, CSIS_OF_NODE_NAME)) |
| 723 | plat_entity = IDX_CSIS; |
| 724 | else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME)) |
| 725 | plat_entity = IDX_FLITE; |
| 726 | else if (!strcmp(node->name, FIMC_OF_NODE_NAME) && |
| 727 | !of_property_read_bool(node, "samsung,lcd-wb")) |
| 728 | plat_entity = IDX_FIMC; |
| 729 | |
| 730 | if (plat_entity >= 0) |
| 731 | ret = fimc_md_register_platform_entity(fmd, pdev, |
| 732 | plat_entity); |
| 733 | put_device(&pdev->dev); |
| 734 | if (ret < 0) |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | return ret; |
| 739 | } |
| 740 | #else |
| 741 | #define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS) |
| 742 | #endif |
| 743 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 744 | static void fimc_md_unregister_entities(struct fimc_md *fmd) |
| 745 | { |
| 746 | int i; |
| 747 | |
| 748 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 749 | if (fmd->fimc[i] == NULL) |
| 750 | continue; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 751 | v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev); |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 752 | fmd->fimc[i]->pipeline_ops = NULL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 753 | fmd->fimc[i] = NULL; |
| 754 | } |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 755 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { |
| 756 | if (fmd->fimc_lite[i] == NULL) |
| 757 | continue; |
| 758 | v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev); |
Sylwester Nawrocki | 248ac36 | 2013-01-11 07:06:28 -0300 | [diff] [blame] | 759 | fmd->fimc_lite[i]->pipeline_ops = NULL; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 760 | fmd->fimc_lite[i] = NULL; |
| 761 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 762 | for (i = 0; i < CSIS_MAX_ENTITIES; i++) { |
| 763 | if (fmd->csis[i].sd == NULL) |
| 764 | continue; |
| 765 | v4l2_device_unregister_subdev(fmd->csis[i].sd); |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 766 | module_put(fmd->csis[i].sd->owner); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 767 | fmd->csis[i].sd = NULL; |
| 768 | } |
| 769 | for (i = 0; i < fmd->num_sensors; i++) { |
| 770 | if (fmd->sensor[i].subdev == NULL) |
| 771 | continue; |
| 772 | fimc_md_unregister_sensor(fmd->sensor[i].subdev); |
| 773 | fmd->sensor[i].subdev = NULL; |
| 774 | } |
Sylwester Nawrocki | 7b43a6f | 2012-12-12 08:16:05 -0300 | [diff] [blame] | 775 | v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n"); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 776 | } |
| 777 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 778 | /** |
| 779 | * __fimc_md_create_fimc_links - create links to all FIMC entities |
| 780 | * @fmd: fimc media device |
| 781 | * @source: the source entity to create links to all fimc entities from |
| 782 | * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null |
| 783 | * @pad: the source entity pad index |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 784 | * @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] | 785 | */ |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 786 | static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd, |
| 787 | struct media_entity *source, |
| 788 | struct v4l2_subdev *sensor, |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 789 | int pad, int link_mask) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 790 | { |
Sylwester Nawrocki | 56bc911 | 2013-02-01 15:00:40 -0300 | [diff] [blame] | 791 | struct fimc_sensor_info *s_info = NULL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 792 | struct media_entity *sink; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 793 | unsigned int flags = 0; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 794 | int ret, i; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 795 | |
| 796 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 797 | if (!fmd->fimc[i]) |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 798 | continue; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 799 | /* |
| 800 | * Some FIMC variants are not fitted with camera capture |
| 801 | * interface. Skip creating a link from sensor for those. |
| 802 | */ |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 803 | if (!fmd->fimc[i]->variant->has_cam_if) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 804 | continue; |
| 805 | |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 806 | flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 807 | |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 808 | sink = &fmd->fimc[i]->vid_cap.subdev.entity; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 809 | ret = media_entity_create_link(source, pad, sink, |
| 810 | FIMC_SD_PAD_SINK, flags); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 811 | if (ret) |
| 812 | return ret; |
| 813 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 814 | /* Notify FIMC capture subdev entity */ |
| 815 | ret = media_entity_call(sink, link_setup, &sink->pads[0], |
| 816 | &source->pads[pad], flags); |
| 817 | if (ret) |
| 818 | break; |
| 819 | |
Sylwester Nawrocki | 542fb08 | 2012-10-25 05:48:19 -0300 | [diff] [blame] | 820 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n", |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 821 | source->name, flags ? '=' : '-', sink->name); |
| 822 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 823 | if (flags == 0 || sensor == NULL) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 824 | continue; |
| 825 | s_info = v4l2_get_subdev_hostdata(sensor); |
| 826 | if (!WARN_ON(s_info == NULL)) { |
| 827 | unsigned long irq_flags; |
| 828 | spin_lock_irqsave(&fmd->slock, irq_flags); |
| 829 | s_info->host = fmd->fimc[i]; |
| 830 | spin_unlock_irqrestore(&fmd->slock, irq_flags); |
| 831 | } |
| 832 | } |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 833 | |
| 834 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { |
| 835 | if (!fmd->fimc_lite[i]) |
| 836 | continue; |
| 837 | |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 838 | if (link_mask & (1 << (i + FIMC_MAX_DEVS))) |
| 839 | flags = MEDIA_LNK_FL_ENABLED; |
| 840 | else |
| 841 | flags = 0; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 842 | |
| 843 | sink = &fmd->fimc_lite[i]->subdev.entity; |
| 844 | ret = media_entity_create_link(source, pad, sink, |
| 845 | FLITE_SD_PAD_SINK, flags); |
| 846 | if (ret) |
| 847 | return ret; |
| 848 | |
| 849 | /* Notify FIMC-LITE subdev entity */ |
| 850 | ret = media_entity_call(sink, link_setup, &sink->pads[0], |
| 851 | &source->pads[pad], flags); |
| 852 | if (ret) |
| 853 | break; |
| 854 | |
Sylwester Nawrocki | 969e877 | 2012-12-07 16:40:08 -0300 | [diff] [blame] | 855 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n", |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 856 | source->name, flags ? '=' : '-', sink->name); |
| 857 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 858 | return 0; |
| 859 | } |
| 860 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 861 | /* Create links from FIMC-LITE source pads to other entities */ |
| 862 | static int __fimc_md_create_flite_source_links(struct fimc_md *fmd) |
| 863 | { |
| 864 | struct media_entity *source, *sink; |
| 865 | unsigned int flags = MEDIA_LNK_FL_ENABLED; |
Sylwester Nawrocki | a26860b | 2012-12-05 13:43:05 -0300 | [diff] [blame] | 866 | int i, ret = 0; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 867 | |
| 868 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { |
| 869 | struct fimc_lite *fimc = fmd->fimc_lite[i]; |
| 870 | if (fimc == NULL) |
| 871 | continue; |
| 872 | source = &fimc->subdev.entity; |
Sylwester Nawrocki | 1bcd704 | 2012-07-26 07:13:08 -0300 | [diff] [blame] | 873 | sink = &fimc->vfd.entity; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 874 | /* FIMC-LITE's subdev and video node */ |
Sylwester Nawrocki | 6319d6a | 2012-11-28 15:41:01 -0300 | [diff] [blame] | 875 | ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA, |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 876 | sink, 0, flags); |
| 877 | if (ret) |
| 878 | break; |
| 879 | /* TODO: create links to other entities */ |
| 880 | } |
| 881 | |
| 882 | return ret; |
| 883 | } |
| 884 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 885 | /** |
| 886 | * fimc_md_create_links - create default links between registered entities |
| 887 | * |
| 888 | * Parallel interface sensor entities are connected directly to FIMC capture |
| 889 | * entities. The sensors using MIPI CSIS bus are connected through immutable |
| 890 | * link with CSI receiver entity specified by mux_id. Any registered CSIS |
| 891 | * entity has a link to each registered FIMC capture entity. Enabled links |
| 892 | * are created by default between each subsequent registered sensor and |
| 893 | * subsequent FIMC capture entity. The number of default active links is |
| 894 | * determined by the number of available sensors or FIMC entities, |
| 895 | * whichever is less. |
| 896 | */ |
| 897 | static int fimc_md_create_links(struct fimc_md *fmd) |
| 898 | { |
Sylwester Nawrocki | a8697ec | 2012-12-05 13:40:04 -0300 | [diff] [blame] | 899 | struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL }; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 900 | struct v4l2_subdev *sensor, *csis; |
Sylwester Nawrocki | 56bc911 | 2013-02-01 15:00:40 -0300 | [diff] [blame] | 901 | struct fimc_source_info *pdata; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 902 | struct fimc_sensor_info *s_info; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 903 | struct media_entity *source, *sink; |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 904 | int i, pad, fimc_id = 0, ret = 0; |
| 905 | u32 flags, link_mask = 0; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 906 | |
| 907 | for (i = 0; i < fmd->num_sensors; i++) { |
| 908 | if (fmd->sensor[i].subdev == NULL) |
| 909 | continue; |
| 910 | |
| 911 | sensor = fmd->sensor[i].subdev; |
| 912 | s_info = v4l2_get_subdev_hostdata(sensor); |
Sylwester Nawrocki | 6612a08 | 2012-09-17 06:00:48 -0300 | [diff] [blame] | 913 | if (!s_info) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 914 | continue; |
| 915 | |
| 916 | source = NULL; |
Sylwester Nawrocki | 6612a08 | 2012-09-17 06:00:48 -0300 | [diff] [blame] | 917 | pdata = &s_info->pdata; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 918 | |
Sylwester Nawrocki | 56bc911 | 2013-02-01 15:00:40 -0300 | [diff] [blame] | 919 | switch (pdata->sensor_bus_type) { |
| 920 | case FIMC_BUS_TYPE_MIPI_CSI2: |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 921 | if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES, |
| 922 | "Wrong CSI channel id: %d\n", pdata->mux_id)) |
| 923 | return -EINVAL; |
| 924 | |
| 925 | csis = fmd->csis[pdata->mux_id].sd; |
| 926 | if (WARN(csis == NULL, |
| 927 | "MIPI-CSI interface specified " |
| 928 | "but s5p-csis module is not loaded!\n")) |
Sylwester Nawrocki | d12392e | 2011-10-20 05:15:59 -0300 | [diff] [blame] | 929 | return -EINVAL; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 930 | |
Andrzej Hajda | 1c9f5bd | 2012-11-22 12:13:27 -0300 | [diff] [blame] | 931 | pad = sensor->entity.num_pads - 1; |
| 932 | ret = media_entity_create_link(&sensor->entity, pad, |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 933 | &csis->entity, CSIS_PAD_SINK, |
| 934 | MEDIA_LNK_FL_IMMUTABLE | |
| 935 | MEDIA_LNK_FL_ENABLED); |
| 936 | if (ret) |
| 937 | return ret; |
| 938 | |
Sylwester Nawrocki | 969e877 | 2012-12-07 16:40:08 -0300 | [diff] [blame] | 939 | v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n", |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 940 | sensor->entity.name, csis->entity.name); |
| 941 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 942 | source = NULL; |
Sylwester Nawrocki | 5d33ee9 | 2012-08-16 14:06:41 -0300 | [diff] [blame] | 943 | csi_sensors[pdata->mux_id] = sensor; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 944 | break; |
| 945 | |
Sylwester Nawrocki | 56bc911 | 2013-02-01 15:00:40 -0300 | [diff] [blame] | 946 | case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656: |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 947 | source = &sensor->entity; |
| 948 | pad = 0; |
| 949 | break; |
| 950 | |
| 951 | default: |
| 952 | v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n", |
Sylwester Nawrocki | 56bc911 | 2013-02-01 15:00:40 -0300 | [diff] [blame] | 953 | pdata->sensor_bus_type); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 954 | return -EINVAL; |
| 955 | } |
| 956 | if (source == NULL) |
| 957 | continue; |
| 958 | |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 959 | link_mask = 1 << fimc_id++; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 960 | ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor, |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 961 | pad, link_mask); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 962 | } |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 963 | |
Sylwester Nawrocki | a8697ec | 2012-12-05 13:40:04 -0300 | [diff] [blame] | 964 | for (i = 0; i < CSIS_MAX_ENTITIES; i++) { |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 965 | if (fmd->csis[i].sd == NULL) |
| 966 | continue; |
| 967 | source = &fmd->csis[i].sd->entity; |
| 968 | pad = CSIS_PAD_SOURCE; |
Sylwester Nawrocki | 5d33ee9 | 2012-08-16 14:06:41 -0300 | [diff] [blame] | 969 | sensor = csi_sensors[i]; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 970 | |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 971 | link_mask = 1 << fimc_id++; |
Sylwester Nawrocki | 5d33ee9 | 2012-08-16 14:06:41 -0300 | [diff] [blame] | 972 | ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor, |
Sylwester Nawrocki | d0da3c3 | 2012-05-16 13:08:30 -0300 | [diff] [blame] | 973 | pad, link_mask); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 974 | } |
| 975 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 976 | /* Create immutable links between each FIMC's subdev and video node */ |
| 977 | flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED; |
| 978 | for (i = 0; i < FIMC_MAX_DEVS; i++) { |
| 979 | if (!fmd->fimc[i]) |
| 980 | continue; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 981 | source = &fmd->fimc[i]->vid_cap.subdev.entity; |
Sylwester Nawrocki | 31d34d9 | 2012-07-26 07:15:42 -0300 | [diff] [blame] | 982 | sink = &fmd->fimc[i]->vid_cap.vfd.entity; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 983 | ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE, |
| 984 | sink, 0, flags); |
| 985 | if (ret) |
| 986 | break; |
| 987 | } |
| 988 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 989 | return __fimc_md_create_flite_source_links(fmd); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 993 | * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 994 | */ |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 995 | static void fimc_md_put_clocks(struct fimc_md *fmd) |
| 996 | { |
| 997 | int i = FIMC_MAX_CAMCLKS; |
| 998 | |
| 999 | while (--i >= 0) { |
Sylwester Nawrocki | 0e23cbb | 2013-01-18 15:34:37 -0300 | [diff] [blame] | 1000 | if (IS_ERR(fmd->camclk[i].clock)) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1001 | continue; |
Sylwester Nawrocki | 0e23cbb | 2013-01-18 15:34:37 -0300 | [diff] [blame] | 1002 | clk_unprepare(fmd->camclk[i].clock); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1003 | clk_put(fmd->camclk[i].clock); |
Sylwester Nawrocki | 0e23cbb | 2013-01-18 15:34:37 -0300 | [diff] [blame] | 1004 | fmd->camclk[i].clock = ERR_PTR(-EINVAL); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1005 | } |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 1006 | |
| 1007 | /* Writeback (PIXELASYNCMx) clocks */ |
| 1008 | for (i = 0; i < FIMC_MAX_WBCLKS; i++) { |
| 1009 | if (IS_ERR(fmd->wbclk[i])) |
| 1010 | continue; |
| 1011 | clk_put(fmd->wbclk[i]); |
| 1012 | fmd->wbclk[i] = ERR_PTR(-EINVAL); |
| 1013 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1014 | } |
| 1015 | |
Sylwester Nawrocki | 0e23cbb | 2013-01-18 15:34:37 -0300 | [diff] [blame] | 1016 | static int fimc_md_get_clocks(struct fimc_md *fmd) |
| 1017 | { |
| 1018 | struct device *dev = NULL; |
| 1019 | char clk_name[32]; |
| 1020 | struct clk *clock; |
| 1021 | int ret, i; |
| 1022 | |
| 1023 | for (i = 0; i < FIMC_MAX_CAMCLKS; i++) |
| 1024 | fmd->camclk[i].clock = ERR_PTR(-EINVAL); |
| 1025 | |
| 1026 | if (fmd->pdev->dev.of_node) |
| 1027 | dev = &fmd->pdev->dev; |
| 1028 | |
| 1029 | for (i = 0; i < FIMC_MAX_CAMCLKS; i++) { |
| 1030 | snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i); |
| 1031 | clock = clk_get(dev, clk_name); |
| 1032 | |
| 1033 | if (IS_ERR(clock)) { |
| 1034 | dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n", |
| 1035 | clk_name); |
| 1036 | ret = PTR_ERR(clock); |
| 1037 | break; |
| 1038 | } |
| 1039 | ret = clk_prepare(clock); |
| 1040 | if (ret < 0) { |
| 1041 | clk_put(clock); |
| 1042 | fmd->camclk[i].clock = ERR_PTR(-EINVAL); |
| 1043 | break; |
| 1044 | } |
| 1045 | fmd->camclk[i].clock = clock; |
| 1046 | } |
| 1047 | if (ret) |
| 1048 | fimc_md_put_clocks(fmd); |
| 1049 | |
Sylwester Nawrocki | 056f4f3 | 2013-03-21 14:43:17 -0300 | [diff] [blame^] | 1050 | if (!fmd->use_isp) |
| 1051 | return 0; |
| 1052 | /* |
| 1053 | * For now get only PIXELASYNCM1 clock (Writeback B/ISP), |
| 1054 | * leave PIXELASYNCM0 out for the LCD Writeback driver. |
| 1055 | */ |
| 1056 | fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL); |
| 1057 | |
| 1058 | for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) { |
| 1059 | snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i); |
| 1060 | clock = clk_get(dev, clk_name); |
| 1061 | if (IS_ERR(clock)) { |
| 1062 | v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n", |
| 1063 | clk_name); |
| 1064 | ret = PTR_ERR(clock); |
| 1065 | break; |
| 1066 | } |
| 1067 | fmd->wbclk[i] = clock; |
| 1068 | } |
| 1069 | if (ret) |
| 1070 | fimc_md_put_clocks(fmd); |
| 1071 | |
Sylwester Nawrocki | 0e23cbb | 2013-01-18 15:34:37 -0300 | [diff] [blame] | 1072 | return ret; |
| 1073 | } |
| 1074 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1075 | static int __fimc_md_set_camclk(struct fimc_md *fmd, |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 1076 | struct fimc_sensor_info *s_info, |
| 1077 | bool on) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1078 | { |
Sylwester Nawrocki | 56bc911 | 2013-02-01 15:00:40 -0300 | [diff] [blame] | 1079 | struct fimc_source_info *pdata = &s_info->pdata; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1080 | struct fimc_camclk_info *camclk; |
| 1081 | int ret = 0; |
| 1082 | |
| 1083 | if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL) |
| 1084 | return -EINVAL; |
| 1085 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1086 | camclk = &fmd->camclk[pdata->clk_id]; |
| 1087 | |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 1088 | dbg("camclk %d, f: %lu, use_count: %d, on: %d", |
| 1089 | pdata->clk_id, pdata->clk_frequency, camclk->use_count, on); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1090 | |
| 1091 | if (on) { |
| 1092 | if (camclk->use_count > 0 && |
| 1093 | camclk->frequency != pdata->clk_frequency) |
| 1094 | return -EINVAL; |
| 1095 | |
| 1096 | if (camclk->use_count++ == 0) { |
| 1097 | clk_set_rate(camclk->clock, pdata->clk_frequency); |
| 1098 | camclk->frequency = pdata->clk_frequency; |
| 1099 | ret = clk_enable(camclk->clock); |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 1100 | dbg("Enabled camclk %d: f: %lu", pdata->clk_id, |
| 1101 | clk_get_rate(camclk->clock)); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1102 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1103 | return ret; |
| 1104 | } |
| 1105 | |
| 1106 | if (WARN_ON(camclk->use_count == 0)) |
| 1107 | return 0; |
| 1108 | |
| 1109 | if (--camclk->use_count == 0) { |
| 1110 | clk_disable(camclk->clock); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1111 | dbg("Disabled camclk %d", pdata->clk_id); |
| 1112 | } |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * fimc_md_set_camclk - peripheral sensor clock setup |
| 1118 | * @sd: sensor subdev to configure sclk_cam clock for |
| 1119 | * @on: 1 to enable or 0 to disable the clock |
| 1120 | * |
| 1121 | * There are 2 separate clock outputs available in the SoC for external |
| 1122 | * image processors. These clocks are shared between all registered FIMC |
| 1123 | * devices to which sensors can be attached, either directly or through |
| 1124 | * the MIPI CSI receiver. The clock is allowed here to be used by |
| 1125 | * multiple sensors concurrently if they use same frequency. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1126 | * This function should only be called when the graph mutex is held. |
| 1127 | */ |
| 1128 | int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on) |
| 1129 | { |
| 1130 | struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd); |
| 1131 | struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity); |
| 1132 | |
| 1133 | return __fimc_md_set_camclk(fmd, s_info, on); |
| 1134 | } |
| 1135 | |
| 1136 | static int fimc_md_link_notify(struct media_pad *source, |
| 1137 | struct media_pad *sink, u32 flags) |
| 1138 | { |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1139 | struct fimc_lite *fimc_lite = NULL; |
| 1140 | struct fimc_dev *fimc = NULL; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 1141 | struct fimc_pipeline *pipeline; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1142 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1143 | struct mutex *lock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1144 | int ret = 0; |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1145 | int ref_count; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1146 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1147 | if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1148 | return 0; |
| 1149 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1150 | sd = media_entity_to_v4l2_subdev(sink->entity); |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 1151 | |
| 1152 | switch (sd->grp_id) { |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 1153 | case GRP_ID_FLITE: |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1154 | fimc_lite = v4l2_get_subdevdata(sd); |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1155 | if (WARN_ON(fimc_lite == NULL)) |
| 1156 | return 0; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1157 | pipeline = &fimc_lite->pipeline; |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1158 | lock = &fimc_lite->lock; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1159 | break; |
Sylwester Nawrocki | 588c87b | 2012-11-27 11:57:42 -0300 | [diff] [blame] | 1160 | case GRP_ID_FIMC: |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 1161 | fimc = v4l2_get_subdevdata(sd); |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1162 | if (WARN_ON(fimc == NULL)) |
| 1163 | return 0; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 1164 | pipeline = &fimc->pipeline; |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1165 | lock = &fimc->lock; |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 1166 | break; |
| 1167 | default: |
| 1168 | return 0; |
| 1169 | } |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1170 | |
| 1171 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1172 | int i; |
| 1173 | mutex_lock(lock); |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame] | 1174 | ret = __fimc_pipeline_close(pipeline); |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1175 | for (i = 0; i < IDX_MAX; i++) |
| 1176 | pipeline->subdevs[i] = NULL; |
| 1177 | if (fimc) |
Sylwester Nawrocki | 0f735f5 | 2012-04-27 09:33:10 -0300 | [diff] [blame] | 1178 | fimc_ctrls_delete(fimc->vid_cap.ctx); |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1179 | mutex_unlock(lock); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1180 | return ret; |
| 1181 | } |
| 1182 | /* |
| 1183 | * Link activation. Enable power of pipeline elements only if the |
| 1184 | * pipeline is already in use, i.e. its video node is opened. |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 1185 | * Recreate the controls destroyed during the link deactivation. |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1186 | */ |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1187 | mutex_lock(lock); |
| 1188 | |
| 1189 | ref_count = fimc ? fimc->vid_cap.refcnt : fimc_lite->ref_count; |
| 1190 | if (ref_count > 0) |
| 1191 | ret = __fimc_pipeline_open(pipeline, source->entity, true); |
| 1192 | if (!ret && fimc) |
| 1193 | ret = fimc_capture_ctrls_create(fimc); |
| 1194 | |
| 1195 | mutex_unlock(lock); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1196 | return ret ? -EPIPE : ret; |
| 1197 | } |
| 1198 | |
| 1199 | static ssize_t fimc_md_sysfs_show(struct device *dev, |
| 1200 | struct device_attribute *attr, char *buf) |
| 1201 | { |
| 1202 | struct platform_device *pdev = to_platform_device(dev); |
| 1203 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 1204 | |
| 1205 | if (fmd->user_subdev_api) |
| 1206 | return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE); |
| 1207 | |
| 1208 | return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE); |
| 1209 | } |
| 1210 | |
| 1211 | static ssize_t fimc_md_sysfs_store(struct device *dev, |
| 1212 | struct device_attribute *attr, |
| 1213 | const char *buf, size_t count) |
| 1214 | { |
| 1215 | struct platform_device *pdev = to_platform_device(dev); |
| 1216 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 1217 | bool subdev_api; |
| 1218 | int i; |
| 1219 | |
| 1220 | if (!strcmp(buf, "vid-dev\n")) |
| 1221 | subdev_api = false; |
| 1222 | else if (!strcmp(buf, "sub-dev\n")) |
| 1223 | subdev_api = true; |
| 1224 | else |
| 1225 | return count; |
| 1226 | |
| 1227 | fmd->user_subdev_api = subdev_api; |
| 1228 | for (i = 0; i < FIMC_MAX_DEVS; i++) |
| 1229 | if (fmd->fimc[i]) |
| 1230 | fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api; |
| 1231 | return count; |
| 1232 | } |
| 1233 | /* |
| 1234 | * This device attribute is to select video pipeline configuration method. |
| 1235 | * There are following valid values: |
| 1236 | * vid-dev - for V4L2 video node API only, subdevice will be configured |
| 1237 | * by the host driver. |
| 1238 | * sub-dev - for media controller API, subdevs must be configured in user |
| 1239 | * space before starting streaming. |
| 1240 | */ |
| 1241 | static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO, |
| 1242 | fimc_md_sysfs_show, fimc_md_sysfs_store); |
| 1243 | |
Sylwester Nawrocki | 4163851 | 2013-03-08 10:41:47 -0300 | [diff] [blame] | 1244 | static int fimc_md_get_pinctrl(struct fimc_md *fmd) |
| 1245 | { |
| 1246 | struct device *dev = &fmd->pdev->dev; |
| 1247 | struct fimc_pinctrl *pctl = &fmd->pinctl; |
| 1248 | |
| 1249 | pctl->pinctrl = devm_pinctrl_get(dev); |
| 1250 | if (IS_ERR(pctl->pinctrl)) |
| 1251 | return PTR_ERR(pctl->pinctrl); |
| 1252 | |
| 1253 | pctl->state_default = pinctrl_lookup_state(pctl->pinctrl, |
| 1254 | PINCTRL_STATE_DEFAULT); |
| 1255 | if (IS_ERR(pctl->state_default)) |
| 1256 | return PTR_ERR(pctl->state_default); |
| 1257 | |
| 1258 | pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl, |
| 1259 | PINCTRL_STATE_IDLE); |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 1263 | static int fimc_md_probe(struct platform_device *pdev) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1264 | { |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1265 | struct device *dev = &pdev->dev; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1266 | struct v4l2_device *v4l2_dev; |
| 1267 | struct fimc_md *fmd; |
| 1268 | int ret; |
| 1269 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1270 | fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1271 | if (!fmd) |
| 1272 | return -ENOMEM; |
| 1273 | |
| 1274 | spin_lock_init(&fmd->slock); |
| 1275 | fmd->pdev = pdev; |
| 1276 | |
| 1277 | strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC", |
| 1278 | sizeof(fmd->media_dev.model)); |
| 1279 | fmd->media_dev.link_notify = fimc_md_link_notify; |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1280 | fmd->media_dev.dev = dev; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1281 | |
| 1282 | v4l2_dev = &fmd->v4l2_dev; |
| 1283 | v4l2_dev->mdev = &fmd->media_dev; |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1284 | v4l2_dev->notify = fimc_sensor_notify; |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1285 | strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name)); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1286 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1287 | ret = v4l2_device_register(dev, &fmd->v4l2_dev); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1288 | if (ret < 0) { |
| 1289 | v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret); |
Sylwester Nawrocki | 6d91a51 | 2012-01-30 11:37:59 -0300 | [diff] [blame] | 1290 | return ret; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1291 | } |
| 1292 | ret = media_device_register(&fmd->media_dev); |
| 1293 | if (ret < 0) { |
| 1294 | v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1295 | goto err_md; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1296 | } |
| 1297 | ret = fimc_md_get_clocks(fmd); |
| 1298 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1299 | goto err_clk; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1300 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1301 | fmd->user_subdev_api = (dev->of_node != NULL); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1302 | |
| 1303 | /* Protect the media graph while we're registering entities */ |
| 1304 | mutex_lock(&fmd->media_dev.graph_mutex); |
| 1305 | |
Sylwester Nawrocki | 4163851 | 2013-03-08 10:41:47 -0300 | [diff] [blame] | 1306 | ret = fimc_md_get_pinctrl(fmd); |
| 1307 | if (ret < 0) { |
| 1308 | if (ret != EPROBE_DEFER) |
| 1309 | dev_err(dev, "Failed to get pinctrl: %d\n", ret); |
| 1310 | goto err_unlock; |
| 1311 | } |
| 1312 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1313 | if (dev->of_node) |
| 1314 | ret = fimc_md_register_of_platform_entities(fmd, dev->of_node); |
| 1315 | else |
| 1316 | ret = bus_for_each_dev(&platform_bus_type, NULL, fmd, |
| 1317 | fimc_md_pdev_match); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1318 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1319 | goto err_unlock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1320 | |
Sylwester Nawrocki | 2b13f7d | 2013-03-29 14:12:39 -0300 | [diff] [blame] | 1321 | if (dev->platform_data || dev->of_node) { |
Sylwester Nawrocki | 5cbf6f16 | 2011-10-07 07:40:00 -0300 | [diff] [blame] | 1322 | ret = fimc_md_register_sensor_entities(fmd); |
| 1323 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1324 | goto err_unlock; |
Sylwester Nawrocki | 5cbf6f16 | 2011-10-07 07:40:00 -0300 | [diff] [blame] | 1325 | } |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1326 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1327 | ret = fimc_md_create_links(fmd); |
| 1328 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1329 | goto err_unlock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1330 | ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev); |
| 1331 | if (ret) |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1332 | goto err_unlock; |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1333 | |
| 1334 | ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1335 | if (ret) |
| 1336 | goto err_unlock; |
| 1337 | |
| 1338 | platform_set_drvdata(pdev, fmd); |
| 1339 | mutex_unlock(&fmd->media_dev.graph_mutex); |
| 1340 | return 0; |
| 1341 | |
| 1342 | err_unlock: |
| 1343 | mutex_unlock(&fmd->media_dev.graph_mutex); |
| 1344 | err_clk: |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1345 | media_device_unregister(&fmd->media_dev); |
| 1346 | fimc_md_put_clocks(fmd); |
| 1347 | fimc_md_unregister_entities(fmd); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1348 | err_md: |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1349 | v4l2_device_unregister(&fmd->v4l2_dev); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1350 | return ret; |
| 1351 | } |
| 1352 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1353 | static int fimc_md_remove(struct platform_device *pdev) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1354 | { |
| 1355 | struct fimc_md *fmd = platform_get_drvdata(pdev); |
| 1356 | |
| 1357 | if (!fmd) |
| 1358 | return 0; |
| 1359 | device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode); |
| 1360 | fimc_md_unregister_entities(fmd); |
| 1361 | media_device_unregister(&fmd->media_dev); |
| 1362 | fimc_md_put_clocks(fmd); |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1363 | return 0; |
| 1364 | } |
| 1365 | |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1366 | static struct platform_device_id fimc_driver_ids[] __always_unused = { |
| 1367 | { .name = "s5p-fimc-md" }, |
| 1368 | { }, |
| 1369 | }; |
| 1370 | MODULE_DEVICE_TABLE(platform, fimc_driver_ids); |
| 1371 | |
| 1372 | static const struct of_device_id fimc_md_of_match[] = { |
| 1373 | { .compatible = "samsung,fimc" }, |
| 1374 | { }, |
| 1375 | }; |
| 1376 | MODULE_DEVICE_TABLE(of, fimc_md_of_match); |
| 1377 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1378 | static struct platform_driver fimc_md_driver = { |
| 1379 | .probe = fimc_md_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1380 | .remove = fimc_md_remove, |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1381 | .driver = { |
Sylwester Nawrocki | e2985a2 | 2013-03-08 12:59:33 -0300 | [diff] [blame] | 1382 | .of_match_table = of_match_ptr(fimc_md_of_match), |
| 1383 | .name = "s5p-fimc-md", |
| 1384 | .owner = THIS_MODULE, |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1385 | } |
| 1386 | }; |
| 1387 | |
Sachin Kamat | 7e566be | 2012-05-26 11:11:54 -0300 | [diff] [blame] | 1388 | static int __init fimc_md_init(void) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1389 | { |
| 1390 | int ret; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 1391 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1392 | request_module("s5p-csis"); |
| 1393 | ret = fimc_register_driver(); |
| 1394 | if (ret) |
| 1395 | return ret; |
Sylwester Nawrocki | ecd9acb | 2012-03-21 09:58:09 -0300 | [diff] [blame] | 1396 | |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1397 | return platform_driver_register(&fimc_md_driver); |
| 1398 | } |
Sachin Kamat | 7e566be | 2012-05-26 11:11:54 -0300 | [diff] [blame] | 1399 | |
| 1400 | static void __exit fimc_md_exit(void) |
Sylwester Nawrocki | d395322 | 2011-09-01 06:01:08 -0300 | [diff] [blame] | 1401 | { |
| 1402 | platform_driver_unregister(&fimc_md_driver); |
| 1403 | fimc_unregister_driver(); |
| 1404 | } |
| 1405 | |
| 1406 | module_init(fimc_md_init); |
| 1407 | module_exit(fimc_md_exit); |
| 1408 | |
| 1409 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); |
| 1410 | MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver"); |
| 1411 | MODULE_LICENSE("GPL"); |
| 1412 | MODULE_VERSION("2.0.1"); |