xref: /linux/drivers/media/i2c/s5k6a3.c (revision 2da68a77)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Samsung S5K6A3 image sensor driver
4  *
5  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/gpio.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of_gpio.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/videodev2.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-subdev.h>
24 
25 #define S5K6A3_SENSOR_MAX_WIDTH		1412
26 #define S5K6A3_SENSOR_MAX_HEIGHT	1412
27 #define S5K6A3_SENSOR_MIN_WIDTH		32
28 #define S5K6A3_SENSOR_MIN_HEIGHT	32
29 
30 #define S5K6A3_DEFAULT_WIDTH		1296
31 #define S5K6A3_DEFAULT_HEIGHT		732
32 
33 #define S5K6A3_DRV_NAME			"S5K6A3"
34 #define S5K6A3_CLK_NAME			"extclk"
35 #define S5K6A3_DEFAULT_CLK_FREQ		24000000U
36 
37 enum {
38 	S5K6A3_SUPP_VDDA,
39 	S5K6A3_SUPP_VDDIO,
40 	S5K6A3_SUPP_AFVDD,
41 	S5K6A3_NUM_SUPPLIES,
42 };
43 
44 /**
45  * struct s5k6a3 - fimc-is sensor data structure
46  * @dev: pointer to this I2C client device structure
47  * @subdev: the image sensor's v4l2 subdev
48  * @pad: subdev media source pad
49  * @supplies: image sensor's voltage regulator supplies
50  * @gpio_reset: GPIO connected to the sensor's reset pin
51  * @lock: mutex protecting the structure's members below
52  * @format: media bus format at the sensor's source pad
53  * @clock: pointer to &struct clk.
54  * @clock_frequency: clock frequency
55  * @power_count: stores state if device is powered
56  */
57 struct s5k6a3 {
58 	struct device *dev;
59 	struct v4l2_subdev subdev;
60 	struct media_pad pad;
61 	struct regulator_bulk_data supplies[S5K6A3_NUM_SUPPLIES];
62 	int gpio_reset;
63 	struct mutex lock;
64 	struct v4l2_mbus_framefmt format;
65 	struct clk *clock;
66 	u32 clock_frequency;
67 	int power_count;
68 };
69 
70 static const char * const s5k6a3_supply_names[] = {
71 	[S5K6A3_SUPP_VDDA]	= "svdda",
72 	[S5K6A3_SUPP_VDDIO]	= "svddio",
73 	[S5K6A3_SUPP_AFVDD]	= "afvdd",
74 };
75 
76 static inline struct s5k6a3 *sd_to_s5k6a3(struct v4l2_subdev *sd)
77 {
78 	return container_of(sd, struct s5k6a3, subdev);
79 }
80 
81 static const struct v4l2_mbus_framefmt s5k6a3_formats[] = {
82 	{
83 		.code = MEDIA_BUS_FMT_SGRBG10_1X10,
84 		.colorspace = V4L2_COLORSPACE_SRGB,
85 		.field = V4L2_FIELD_NONE,
86 	}
87 };
88 
89 static const struct v4l2_mbus_framefmt *find_sensor_format(
90 	struct v4l2_mbus_framefmt *mf)
91 {
92 	int i;
93 
94 	for (i = 0; i < ARRAY_SIZE(s5k6a3_formats); i++)
95 		if (mf->code == s5k6a3_formats[i].code)
96 			return &s5k6a3_formats[i];
97 
98 	return &s5k6a3_formats[0];
99 }
100 
101 static int s5k6a3_enum_mbus_code(struct v4l2_subdev *sd,
102 				  struct v4l2_subdev_state *sd_state,
103 				  struct v4l2_subdev_mbus_code_enum *code)
104 {
105 	if (code->index >= ARRAY_SIZE(s5k6a3_formats))
106 		return -EINVAL;
107 
108 	code->code = s5k6a3_formats[code->index].code;
109 	return 0;
110 }
111 
112 static void s5k6a3_try_format(struct v4l2_mbus_framefmt *mf)
113 {
114 	const struct v4l2_mbus_framefmt *fmt;
115 
116 	fmt = find_sensor_format(mf);
117 	mf->code = fmt->code;
118 	mf->field = V4L2_FIELD_NONE;
119 	v4l_bound_align_image(&mf->width, S5K6A3_SENSOR_MIN_WIDTH,
120 			      S5K6A3_SENSOR_MAX_WIDTH, 0,
121 			      &mf->height, S5K6A3_SENSOR_MIN_HEIGHT,
122 			      S5K6A3_SENSOR_MAX_HEIGHT, 0, 0);
123 }
124 
125 static struct v4l2_mbus_framefmt *__s5k6a3_get_format(
126 		struct s5k6a3 *sensor, struct v4l2_subdev_state *sd_state,
127 		u32 pad, enum v4l2_subdev_format_whence which)
128 {
129 	if (which == V4L2_SUBDEV_FORMAT_TRY)
130 		return sd_state ? v4l2_subdev_get_try_format(&sensor->subdev,
131 							     sd_state, pad) : NULL;
132 
133 	return &sensor->format;
134 }
135 
136 static int s5k6a3_set_fmt(struct v4l2_subdev *sd,
137 				  struct v4l2_subdev_state *sd_state,
138 				  struct v4l2_subdev_format *fmt)
139 {
140 	struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
141 	struct v4l2_mbus_framefmt *mf;
142 
143 	s5k6a3_try_format(&fmt->format);
144 
145 	mf = __s5k6a3_get_format(sensor, sd_state, fmt->pad, fmt->which);
146 	if (mf) {
147 		mutex_lock(&sensor->lock);
148 		*mf = fmt->format;
149 		mutex_unlock(&sensor->lock);
150 	}
151 	return 0;
152 }
153 
154 static int s5k6a3_get_fmt(struct v4l2_subdev *sd,
155 			  struct v4l2_subdev_state *sd_state,
156 			  struct v4l2_subdev_format *fmt)
157 {
158 	struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
159 	struct v4l2_mbus_framefmt *mf;
160 
161 	mf = __s5k6a3_get_format(sensor, sd_state, fmt->pad, fmt->which);
162 
163 	mutex_lock(&sensor->lock);
164 	fmt->format = *mf;
165 	mutex_unlock(&sensor->lock);
166 	return 0;
167 }
168 
169 static const struct v4l2_subdev_pad_ops s5k6a3_pad_ops = {
170 	.enum_mbus_code	= s5k6a3_enum_mbus_code,
171 	.get_fmt	= s5k6a3_get_fmt,
172 	.set_fmt	= s5k6a3_set_fmt,
173 };
174 
175 static int s5k6a3_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
176 {
177 	struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd,
178 								       fh->state,
179 								       0);
180 
181 	*format		= s5k6a3_formats[0];
182 	format->width	= S5K6A3_DEFAULT_WIDTH;
183 	format->height	= S5K6A3_DEFAULT_HEIGHT;
184 
185 	return 0;
186 }
187 
188 static const struct v4l2_subdev_internal_ops s5k6a3_sd_internal_ops = {
189 	.open = s5k6a3_open,
190 };
191 
192 static 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 		goto error_rpm_put;
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_clk;
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 
229 error_clk:
230 	clk_disable_unprepare(sensor->clock);
231 error_reg_dis:
232 	for (--i; i >= 0; --i)
233 		regulator_disable(sensor->supplies[i].consumer);
234 error_rpm_put:
235 	pm_runtime_put(sensor->dev);
236 	return ret;
237 }
238 
239 static int __s5k6a3_power_off(struct s5k6a3 *sensor)
240 {
241 	int i;
242 
243 	gpio_set_value(sensor->gpio_reset, 0);
244 
245 	for (i = S5K6A3_NUM_SUPPLIES - 1; i >= 0; i--)
246 		regulator_disable(sensor->supplies[i].consumer);
247 
248 	clk_disable_unprepare(sensor->clock);
249 	pm_runtime_put(sensor->dev);
250 	return 0;
251 }
252 
253 static int s5k6a3_s_power(struct v4l2_subdev *sd, int on)
254 {
255 	struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
256 	int ret = 0;
257 
258 	mutex_lock(&sensor->lock);
259 
260 	if (sensor->power_count == !on) {
261 		if (on)
262 			ret = __s5k6a3_power_on(sensor);
263 		else
264 			ret = __s5k6a3_power_off(sensor);
265 
266 		if (ret == 0)
267 			sensor->power_count += on ? 1 : -1;
268 	}
269 
270 	mutex_unlock(&sensor->lock);
271 	return ret;
272 }
273 
274 static const struct v4l2_subdev_core_ops s5k6a3_core_ops = {
275 	.s_power = s5k6a3_s_power,
276 };
277 
278 static const struct v4l2_subdev_ops s5k6a3_subdev_ops = {
279 	.core = &s5k6a3_core_ops,
280 	.pad = &s5k6a3_pad_ops,
281 };
282 
283 static int s5k6a3_probe(struct i2c_client *client)
284 {
285 	struct device *dev = &client->dev;
286 	struct s5k6a3 *sensor;
287 	struct v4l2_subdev *sd;
288 	int gpio, i, ret;
289 
290 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
291 	if (!sensor)
292 		return -ENOMEM;
293 
294 	mutex_init(&sensor->lock);
295 	sensor->gpio_reset = -EINVAL;
296 	sensor->clock = ERR_PTR(-EINVAL);
297 	sensor->dev = dev;
298 
299 	sensor->clock = devm_clk_get(sensor->dev, S5K6A3_CLK_NAME);
300 	if (IS_ERR(sensor->clock))
301 		return PTR_ERR(sensor->clock);
302 
303 	gpio = of_get_gpio_flags(dev->of_node, 0, NULL);
304 	if (!gpio_is_valid(gpio))
305 		return gpio;
306 
307 	ret = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_LOW,
308 						S5K6A3_DRV_NAME);
309 	if (ret < 0)
310 		return ret;
311 
312 	sensor->gpio_reset = gpio;
313 
314 	if (of_property_read_u32(dev->of_node, "clock-frequency",
315 				 &sensor->clock_frequency)) {
316 		sensor->clock_frequency = S5K6A3_DEFAULT_CLK_FREQ;
317 		dev_info(dev, "using default %u Hz clock frequency\n",
318 					sensor->clock_frequency);
319 	}
320 
321 	for (i = 0; i < S5K6A3_NUM_SUPPLIES; i++)
322 		sensor->supplies[i].supply = s5k6a3_supply_names[i];
323 
324 	ret = devm_regulator_bulk_get(&client->dev, S5K6A3_NUM_SUPPLIES,
325 				      sensor->supplies);
326 	if (ret < 0)
327 		return ret;
328 
329 	sd = &sensor->subdev;
330 	v4l2_i2c_subdev_init(sd, client, &s5k6a3_subdev_ops);
331 	sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
332 	sd->internal_ops = &s5k6a3_sd_internal_ops;
333 
334 	sensor->format.code = s5k6a3_formats[0].code;
335 	sensor->format.width = S5K6A3_DEFAULT_WIDTH;
336 	sensor->format.height = S5K6A3_DEFAULT_HEIGHT;
337 
338 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
339 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
340 	ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
341 	if (ret < 0)
342 		return ret;
343 
344 	pm_runtime_no_callbacks(dev);
345 	pm_runtime_enable(dev);
346 
347 	ret = v4l2_async_register_subdev(sd);
348 
349 	if (ret < 0) {
350 		pm_runtime_disable(&client->dev);
351 		media_entity_cleanup(&sd->entity);
352 	}
353 
354 	return ret;
355 }
356 
357 static void s5k6a3_remove(struct i2c_client *client)
358 {
359 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
360 
361 	pm_runtime_disable(&client->dev);
362 	v4l2_async_unregister_subdev(sd);
363 	media_entity_cleanup(&sd->entity);
364 }
365 
366 static const struct i2c_device_id s5k6a3_ids[] = {
367 	{ }
368 };
369 MODULE_DEVICE_TABLE(i2c, s5k6a3_ids);
370 
371 #ifdef CONFIG_OF
372 static const struct of_device_id s5k6a3_of_match[] = {
373 	{ .compatible = "samsung,s5k6a3" },
374 	{ /* sentinel */ }
375 };
376 MODULE_DEVICE_TABLE(of, s5k6a3_of_match);
377 #endif
378 
379 static struct i2c_driver s5k6a3_driver = {
380 	.driver = {
381 		.of_match_table	= of_match_ptr(s5k6a3_of_match),
382 		.name		= S5K6A3_DRV_NAME,
383 	},
384 	.probe_new	= s5k6a3_probe,
385 	.remove		= s5k6a3_remove,
386 	.id_table	= s5k6a3_ids,
387 };
388 
389 module_i2c_driver(s5k6a3_driver);
390 
391 MODULE_DESCRIPTION("S5K6A3 image sensor subdev driver");
392 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
393 MODULE_LICENSE("GPL v2");
394