blob: 2e140272794b2589f40ecf45628e325c06678af1 [file] [log] [blame]
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -03001/*
2 * Samsung S5K6A3 image sensor driver
3 *
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * Author: 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 version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/gpio.h>
17#include <linux/i2c.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/of_gpio.h>
21#include <linux/pm_runtime.h>
22#include <linux/regulator/consumer.h>
23#include <linux/slab.h>
24#include <linux/videodev2.h>
25#include <media/v4l2-async.h>
26#include <media/v4l2-subdev.h>
27
28#define S5K6A3_SENSOR_MAX_WIDTH 1412
29#define S5K6A3_SENSOR_MAX_HEIGHT 1412
30#define S5K6A3_SENSOR_MIN_WIDTH 32
31#define S5K6A3_SENSOR_MIN_HEIGHT 32
32
33#define S5K6A3_DEFAULT_WIDTH 1296
34#define S5K6A3_DEFAULT_HEIGHT 732
35
36#define S5K6A3_DRV_NAME "S5K6A3"
37#define S5K6A3_CLK_NAME "extclk"
38#define S5K6A3_DEFAULT_CLK_FREQ 24000000U
39
40enum {
41 S5K6A3_SUPP_VDDA,
42 S5K6A3_SUPP_VDDIO,
43 S5K6A3_SUPP_AFVDD,
44 S5K6A3_NUM_SUPPLIES,
45};
46
47/**
48 * struct s5k6a3 - fimc-is sensor data structure
49 * @dev: pointer to this I2C client device structure
50 * @subdev: the image sensor's v4l2 subdev
51 * @pad: subdev media source pad
52 * @supplies: image sensor's voltage regulator supplies
53 * @gpio_reset: GPIO connected to the sensor's reset pin
54 * @lock: mutex protecting the structure's members below
55 * @format: media bus format at the sensor's source pad
Mauro Carvalho Chehab7cd18362017-11-29 08:41:06 -050056 * @clock: pointer to &struct clk.
57 * @clock_frequency: clock frequency
58 * @power_count: stores state if device is powered
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -030059 */
60struct s5k6a3 {
61 struct device *dev;
62 struct v4l2_subdev subdev;
63 struct media_pad pad;
64 struct regulator_bulk_data supplies[S5K6A3_NUM_SUPPLIES];
65 int gpio_reset;
66 struct mutex lock;
67 struct v4l2_mbus_framefmt format;
68 struct clk *clock;
69 u32 clock_frequency;
70 int power_count;
71};
72
73static const char * const s5k6a3_supply_names[] = {
74 [S5K6A3_SUPP_VDDA] = "svdda",
75 [S5K6A3_SUPP_VDDIO] = "svddio",
76 [S5K6A3_SUPP_AFVDD] = "afvdd",
77};
78
79static inline struct s5k6a3 *sd_to_s5k6a3(struct v4l2_subdev *sd)
80{
81 return container_of(sd, struct s5k6a3, subdev);
82}
83
84static const struct v4l2_mbus_framefmt s5k6a3_formats[] = {
85 {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -030086 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -030087 .colorspace = V4L2_COLORSPACE_SRGB,
88 .field = V4L2_FIELD_NONE,
89 }
90};
91
92static const struct v4l2_mbus_framefmt *find_sensor_format(
93 struct v4l2_mbus_framefmt *mf)
94{
95 int i;
96
97 for (i = 0; i < ARRAY_SIZE(s5k6a3_formats); i++)
98 if (mf->code == s5k6a3_formats[i].code)
99 return &s5k6a3_formats[i];
100
101 return &s5k6a3_formats[0];
102}
103
104static int s5k6a3_enum_mbus_code(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800105 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300106 struct v4l2_subdev_mbus_code_enum *code)
107{
108 if (code->index >= ARRAY_SIZE(s5k6a3_formats))
109 return -EINVAL;
110
111 code->code = s5k6a3_formats[code->index].code;
112 return 0;
113}
114
115static void s5k6a3_try_format(struct v4l2_mbus_framefmt *mf)
116{
117 const struct v4l2_mbus_framefmt *fmt;
118
119 fmt = find_sensor_format(mf);
120 mf->code = fmt->code;
Laurent Pinchartd53ce5a2014-05-19 12:04:32 -0300121 mf->field = V4L2_FIELD_NONE;
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300122 v4l_bound_align_image(&mf->width, S5K6A3_SENSOR_MIN_WIDTH,
123 S5K6A3_SENSOR_MAX_WIDTH, 0,
124 &mf->height, S5K6A3_SENSOR_MIN_HEIGHT,
125 S5K6A3_SENSOR_MAX_HEIGHT, 0, 0);
126}
127
128static struct v4l2_mbus_framefmt *__s5k6a3_get_format(
Hans Verkuilf7234132015-03-04 01:47:54 -0800129 struct s5k6a3 *sensor, struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300130 u32 pad, enum v4l2_subdev_format_whence which)
131{
132 if (which == V4L2_SUBDEV_FORMAT_TRY)
Hans Verkuilf7234132015-03-04 01:47:54 -0800133 return cfg ? v4l2_subdev_get_try_format(&sensor->subdev, cfg, pad) : NULL;
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300134
135 return &sensor->format;
136}
137
138static int s5k6a3_set_fmt(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800139 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300140 struct v4l2_subdev_format *fmt)
141{
142 struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
143 struct v4l2_mbus_framefmt *mf;
144
145 s5k6a3_try_format(&fmt->format);
146
Hans Verkuilf7234132015-03-04 01:47:54 -0800147 mf = __s5k6a3_get_format(sensor, cfg, fmt->pad, fmt->which);
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300148 if (mf) {
149 mutex_lock(&sensor->lock);
Jacek Anaszewski4e639592016-01-18 14:10:25 -0200150 *mf = fmt->format;
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300151 mutex_unlock(&sensor->lock);
152 }
153 return 0;
154}
155
156static int s5k6a3_get_fmt(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800157 struct v4l2_subdev_pad_config *cfg,
158 struct v4l2_subdev_format *fmt)
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300159{
160 struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
161 struct v4l2_mbus_framefmt *mf;
162
Hans Verkuilf7234132015-03-04 01:47:54 -0800163 mf = __s5k6a3_get_format(sensor, cfg, fmt->pad, fmt->which);
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300164
165 mutex_lock(&sensor->lock);
166 fmt->format = *mf;
167 mutex_unlock(&sensor->lock);
168 return 0;
169}
170
Bhumika Goyal7c137c62016-12-12 05:45:32 -0200171static const struct v4l2_subdev_pad_ops s5k6a3_pad_ops = {
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300172 .enum_mbus_code = s5k6a3_enum_mbus_code,
173 .get_fmt = s5k6a3_get_fmt,
174 .set_fmt = s5k6a3_set_fmt,
175};
176
177static int s5k6a3_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
178{
Hans Verkuilf7234132015-03-04 01:47:54 -0800179 struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300180
181 *format = s5k6a3_formats[0];
182 format->width = S5K6A3_DEFAULT_WIDTH;
183 format->height = S5K6A3_DEFAULT_HEIGHT;
184
185 return 0;
186}
187
188static const struct v4l2_subdev_internal_ops s5k6a3_sd_internal_ops = {
189 .open = s5k6a3_open,
190};
191
192static int __s5k6a3_power_on(struct s5k6a3 *sensor)
193{
194 int i = S5K6A3_SUPP_VDDA;
195 int ret;
196
197 ret = clk_set_rate(sensor->clock, sensor->clock_frequency);
198 if (ret < 0)
199 return ret;
200
201 ret = pm_runtime_get(sensor->dev);
202 if (ret < 0)
203 return ret;
204
205 ret = regulator_enable(sensor->supplies[i].consumer);
206 if (ret < 0)
207 goto error_rpm_put;
208
209 ret = clk_prepare_enable(sensor->clock);
210 if (ret < 0)
211 goto error_reg_dis;
212
213 for (i++; i < S5K6A3_NUM_SUPPLIES; i++) {
214 ret = regulator_enable(sensor->supplies[i].consumer);
215 if (ret < 0)
216 goto error_reg_dis;
217 }
218
219 gpio_set_value(sensor->gpio_reset, 1);
220 usleep_range(600, 800);
221 gpio_set_value(sensor->gpio_reset, 0);
222 usleep_range(600, 800);
223 gpio_set_value(sensor->gpio_reset, 1);
224
225 /* Delay needed for the sensor initialization */
226 msleep(20);
227 return 0;
228
229error_reg_dis:
230 for (--i; i >= 0; --i)
231 regulator_disable(sensor->supplies[i].consumer);
232error_rpm_put:
233 pm_runtime_put(sensor->dev);
234 return ret;
235}
236
237static int __s5k6a3_power_off(struct s5k6a3 *sensor)
238{
239 int i;
240
241 gpio_set_value(sensor->gpio_reset, 0);
242
243 for (i = S5K6A3_NUM_SUPPLIES - 1; i >= 0; i--)
244 regulator_disable(sensor->supplies[i].consumer);
245
246 clk_disable_unprepare(sensor->clock);
247 pm_runtime_put(sensor->dev);
248 return 0;
249}
250
251static int s5k6a3_s_power(struct v4l2_subdev *sd, int on)
252{
253 struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
254 int ret = 0;
255
256 mutex_lock(&sensor->lock);
257
258 if (sensor->power_count == !on) {
259 if (on)
260 ret = __s5k6a3_power_on(sensor);
261 else
262 ret = __s5k6a3_power_off(sensor);
263
264 if (ret == 0)
265 sensor->power_count += on ? 1 : -1;
266 }
267
268 mutex_unlock(&sensor->lock);
269 return ret;
270}
271
Bhumika Goyal7c137c62016-12-12 05:45:32 -0200272static const struct v4l2_subdev_core_ops s5k6a3_core_ops = {
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300273 .s_power = s5k6a3_s_power,
274};
275
Bhumika Goyal7c137c62016-12-12 05:45:32 -0200276static const struct v4l2_subdev_ops s5k6a3_subdev_ops = {
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300277 .core = &s5k6a3_core_ops,
278 .pad = &s5k6a3_pad_ops,
279};
280
281static int s5k6a3_probe(struct i2c_client *client,
282 const struct i2c_device_id *id)
283{
284 struct device *dev = &client->dev;
285 struct s5k6a3 *sensor;
286 struct v4l2_subdev *sd;
287 int gpio, i, ret;
288
289 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
290 if (!sensor)
291 return -ENOMEM;
292
293 mutex_init(&sensor->lock);
294 sensor->gpio_reset = -EINVAL;
295 sensor->clock = ERR_PTR(-EINVAL);
296 sensor->dev = dev;
297
298 sensor->clock = devm_clk_get(sensor->dev, S5K6A3_CLK_NAME);
299 if (IS_ERR(sensor->clock))
300 return PTR_ERR(sensor->clock);
301
302 gpio = of_get_gpio_flags(dev->of_node, 0, NULL);
303 if (!gpio_is_valid(gpio))
304 return gpio;
305
306 ret = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_LOW,
307 S5K6A3_DRV_NAME);
308 if (ret < 0)
309 return ret;
310
311 sensor->gpio_reset = gpio;
312
313 if (of_property_read_u32(dev->of_node, "clock-frequency",
314 &sensor->clock_frequency)) {
315 sensor->clock_frequency = S5K6A3_DEFAULT_CLK_FREQ;
316 dev_info(dev, "using default %u Hz clock frequency\n",
317 sensor->clock_frequency);
318 }
319
320 for (i = 0; i < S5K6A3_NUM_SUPPLIES; i++)
321 sensor->supplies[i].supply = s5k6a3_supply_names[i];
322
323 ret = devm_regulator_bulk_get(&client->dev, S5K6A3_NUM_SUPPLIES,
324 sensor->supplies);
325 if (ret < 0)
326 return ret;
327
328 sd = &sensor->subdev;
329 v4l2_i2c_subdev_init(sd, client, &s5k6a3_subdev_ops);
330 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
331 sd->internal_ops = &s5k6a3_sd_internal_ops;
332
333 sensor->format.code = s5k6a3_formats[0].code;
334 sensor->format.width = S5K6A3_DEFAULT_WIDTH;
335 sensor->format.height = S5K6A3_DEFAULT_HEIGHT;
336
Sylwester Nawrocki654fa102016-09-01 08:47:06 -0300337 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300338 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200339 ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300340 if (ret < 0)
341 return ret;
342
343 pm_runtime_no_callbacks(dev);
344 pm_runtime_enable(dev);
345
346 ret = v4l2_async_register_subdev(sd);
347
348 if (ret < 0) {
349 pm_runtime_disable(&client->dev);
350 media_entity_cleanup(&sd->entity);
351 }
352
353 return ret;
354}
355
356static int s5k6a3_remove(struct i2c_client *client)
357{
358 struct v4l2_subdev *sd = i2c_get_clientdata(client);
359
360 pm_runtime_disable(&client->dev);
361 v4l2_async_unregister_subdev(sd);
362 media_entity_cleanup(&sd->entity);
363 return 0;
364}
365
366static const struct i2c_device_id s5k6a3_ids[] = {
367 { }
368};
Javier Martinez Canillased8d1cf2015-07-30 13:18:29 -0300369MODULE_DEVICE_TABLE(i2c, s5k6a3_ids);
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300370
371#ifdef CONFIG_OF
372static const struct of_device_id s5k6a3_of_match[] = {
373 { .compatible = "samsung,s5k6a3" },
374 { /* sentinel */ }
375};
376MODULE_DEVICE_TABLE(of, s5k6a3_of_match);
377#endif
378
379static struct i2c_driver s5k6a3_driver = {
380 .driver = {
381 .of_match_table = of_match_ptr(s5k6a3_of_match),
382 .name = S5K6A3_DRV_NAME,
Sylwester Nawrocki814b4dd2013-08-09 15:56:00 -0300383 },
384 .probe = s5k6a3_probe,
385 .remove = s5k6a3_remove,
386 .id_table = s5k6a3_ids,
387};
388
389module_i2c_driver(s5k6a3_driver);
390
391MODULE_DESCRIPTION("S5K6A3 image sensor subdev driver");
392MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
393MODULE_LICENSE("GPL v2");