xref: /openbmc/linux/drivers/media/i2c/s5k6a3.c (revision 157282a5c6273976b31cf4ba4b1c46663f6519d1)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2814b4dd9SSylwester Nawrocki /*
3814b4dd9SSylwester Nawrocki  * Samsung S5K6A3 image sensor driver
4814b4dd9SSylwester Nawrocki  *
5814b4dd9SSylwester Nawrocki  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6814b4dd9SSylwester Nawrocki  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7814b4dd9SSylwester Nawrocki  */
8814b4dd9SSylwester Nawrocki 
9814b4dd9SSylwester Nawrocki #include <linux/clk.h>
10814b4dd9SSylwester Nawrocki #include <linux/delay.h>
11814b4dd9SSylwester Nawrocki #include <linux/device.h>
12814b4dd9SSylwester Nawrocki #include <linux/errno.h>
13814b4dd9SSylwester Nawrocki #include <linux/gpio.h>
14814b4dd9SSylwester Nawrocki #include <linux/i2c.h>
15814b4dd9SSylwester Nawrocki #include <linux/kernel.h>
16814b4dd9SSylwester Nawrocki #include <linux/module.h>
17814b4dd9SSylwester Nawrocki #include <linux/of_gpio.h>
18814b4dd9SSylwester Nawrocki #include <linux/pm_runtime.h>
19814b4dd9SSylwester Nawrocki #include <linux/regulator/consumer.h>
20814b4dd9SSylwester Nawrocki #include <linux/slab.h>
21814b4dd9SSylwester Nawrocki #include <linux/videodev2.h>
22814b4dd9SSylwester Nawrocki #include <media/v4l2-async.h>
23814b4dd9SSylwester Nawrocki #include <media/v4l2-subdev.h>
24814b4dd9SSylwester Nawrocki 
25814b4dd9SSylwester Nawrocki #define S5K6A3_SENSOR_MAX_WIDTH		1412
26814b4dd9SSylwester Nawrocki #define S5K6A3_SENSOR_MAX_HEIGHT	1412
27814b4dd9SSylwester Nawrocki #define S5K6A3_SENSOR_MIN_WIDTH		32
28814b4dd9SSylwester Nawrocki #define S5K6A3_SENSOR_MIN_HEIGHT	32
29814b4dd9SSylwester Nawrocki 
30814b4dd9SSylwester Nawrocki #define S5K6A3_DEFAULT_WIDTH		1296
31814b4dd9SSylwester Nawrocki #define S5K6A3_DEFAULT_HEIGHT		732
32814b4dd9SSylwester Nawrocki 
33814b4dd9SSylwester Nawrocki #define S5K6A3_DRV_NAME			"S5K6A3"
34814b4dd9SSylwester Nawrocki #define S5K6A3_CLK_NAME			"extclk"
35814b4dd9SSylwester Nawrocki #define S5K6A3_DEFAULT_CLK_FREQ		24000000U
36814b4dd9SSylwester Nawrocki 
37814b4dd9SSylwester Nawrocki enum {
38814b4dd9SSylwester Nawrocki 	S5K6A3_SUPP_VDDA,
39814b4dd9SSylwester Nawrocki 	S5K6A3_SUPP_VDDIO,
40814b4dd9SSylwester Nawrocki 	S5K6A3_SUPP_AFVDD,
41814b4dd9SSylwester Nawrocki 	S5K6A3_NUM_SUPPLIES,
42814b4dd9SSylwester Nawrocki };
43814b4dd9SSylwester Nawrocki 
44814b4dd9SSylwester Nawrocki /**
45814b4dd9SSylwester Nawrocki  * struct s5k6a3 - fimc-is sensor data structure
46814b4dd9SSylwester Nawrocki  * @dev: pointer to this I2C client device structure
47814b4dd9SSylwester Nawrocki  * @subdev: the image sensor's v4l2 subdev
48814b4dd9SSylwester Nawrocki  * @pad: subdev media source pad
49814b4dd9SSylwester Nawrocki  * @supplies: image sensor's voltage regulator supplies
50814b4dd9SSylwester Nawrocki  * @gpio_reset: GPIO connected to the sensor's reset pin
51814b4dd9SSylwester Nawrocki  * @lock: mutex protecting the structure's members below
52814b4dd9SSylwester Nawrocki  * @format: media bus format at the sensor's source pad
537cd18369SMauro Carvalho Chehab  * @clock: pointer to &struct clk.
547cd18369SMauro Carvalho Chehab  * @clock_frequency: clock frequency
557cd18369SMauro Carvalho Chehab  * @power_count: stores state if device is powered
56814b4dd9SSylwester Nawrocki  */
57814b4dd9SSylwester Nawrocki struct s5k6a3 {
58814b4dd9SSylwester Nawrocki 	struct device *dev;
59814b4dd9SSylwester Nawrocki 	struct v4l2_subdev subdev;
60814b4dd9SSylwester Nawrocki 	struct media_pad pad;
61814b4dd9SSylwester Nawrocki 	struct regulator_bulk_data supplies[S5K6A3_NUM_SUPPLIES];
62814b4dd9SSylwester Nawrocki 	int gpio_reset;
63814b4dd9SSylwester Nawrocki 	struct mutex lock;
64814b4dd9SSylwester Nawrocki 	struct v4l2_mbus_framefmt format;
65814b4dd9SSylwester Nawrocki 	struct clk *clock;
66814b4dd9SSylwester Nawrocki 	u32 clock_frequency;
67814b4dd9SSylwester Nawrocki 	int power_count;
68814b4dd9SSylwester Nawrocki };
69814b4dd9SSylwester Nawrocki 
70814b4dd9SSylwester Nawrocki static const char * const s5k6a3_supply_names[] = {
71814b4dd9SSylwester Nawrocki 	[S5K6A3_SUPP_VDDA]	= "svdda",
72814b4dd9SSylwester Nawrocki 	[S5K6A3_SUPP_VDDIO]	= "svddio",
73814b4dd9SSylwester Nawrocki 	[S5K6A3_SUPP_AFVDD]	= "afvdd",
74814b4dd9SSylwester Nawrocki };
75814b4dd9SSylwester Nawrocki 
76814b4dd9SSylwester Nawrocki static inline struct s5k6a3 *sd_to_s5k6a3(struct v4l2_subdev *sd)
77814b4dd9SSylwester Nawrocki {
78814b4dd9SSylwester Nawrocki 	return container_of(sd, struct s5k6a3, subdev);
79814b4dd9SSylwester Nawrocki }
80814b4dd9SSylwester Nawrocki 
81814b4dd9SSylwester Nawrocki static const struct v4l2_mbus_framefmt s5k6a3_formats[] = {
82814b4dd9SSylwester Nawrocki 	{
83f5fe58fdSBoris BREZILLON 		.code = MEDIA_BUS_FMT_SGRBG10_1X10,
84814b4dd9SSylwester Nawrocki 		.colorspace = V4L2_COLORSPACE_SRGB,
85814b4dd9SSylwester Nawrocki 		.field = V4L2_FIELD_NONE,
86814b4dd9SSylwester Nawrocki 	}
87814b4dd9SSylwester Nawrocki };
88814b4dd9SSylwester Nawrocki 
89814b4dd9SSylwester Nawrocki static const struct v4l2_mbus_framefmt *find_sensor_format(
90814b4dd9SSylwester Nawrocki 	struct v4l2_mbus_framefmt *mf)
91814b4dd9SSylwester Nawrocki {
92814b4dd9SSylwester Nawrocki 	int i;
93814b4dd9SSylwester Nawrocki 
94814b4dd9SSylwester Nawrocki 	for (i = 0; i < ARRAY_SIZE(s5k6a3_formats); i++)
95814b4dd9SSylwester Nawrocki 		if (mf->code == s5k6a3_formats[i].code)
96814b4dd9SSylwester Nawrocki 			return &s5k6a3_formats[i];
97814b4dd9SSylwester Nawrocki 
98814b4dd9SSylwester Nawrocki 	return &s5k6a3_formats[0];
99814b4dd9SSylwester Nawrocki }
100814b4dd9SSylwester Nawrocki 
101814b4dd9SSylwester Nawrocki static int s5k6a3_enum_mbus_code(struct v4l2_subdev *sd,
102f7234138SHans Verkuil 				  struct v4l2_subdev_pad_config *cfg,
103814b4dd9SSylwester Nawrocki 				  struct v4l2_subdev_mbus_code_enum *code)
104814b4dd9SSylwester Nawrocki {
105814b4dd9SSylwester Nawrocki 	if (code->index >= ARRAY_SIZE(s5k6a3_formats))
106814b4dd9SSylwester Nawrocki 		return -EINVAL;
107814b4dd9SSylwester Nawrocki 
108814b4dd9SSylwester Nawrocki 	code->code = s5k6a3_formats[code->index].code;
109814b4dd9SSylwester Nawrocki 	return 0;
110814b4dd9SSylwester Nawrocki }
111814b4dd9SSylwester Nawrocki 
112814b4dd9SSylwester Nawrocki static void s5k6a3_try_format(struct v4l2_mbus_framefmt *mf)
113814b4dd9SSylwester Nawrocki {
114814b4dd9SSylwester Nawrocki 	const struct v4l2_mbus_framefmt *fmt;
115814b4dd9SSylwester Nawrocki 
116814b4dd9SSylwester Nawrocki 	fmt = find_sensor_format(mf);
117814b4dd9SSylwester Nawrocki 	mf->code = fmt->code;
118d53ce5acSLaurent Pinchart 	mf->field = V4L2_FIELD_NONE;
119814b4dd9SSylwester Nawrocki 	v4l_bound_align_image(&mf->width, S5K6A3_SENSOR_MIN_WIDTH,
120814b4dd9SSylwester Nawrocki 			      S5K6A3_SENSOR_MAX_WIDTH, 0,
121814b4dd9SSylwester Nawrocki 			      &mf->height, S5K6A3_SENSOR_MIN_HEIGHT,
122814b4dd9SSylwester Nawrocki 			      S5K6A3_SENSOR_MAX_HEIGHT, 0, 0);
123814b4dd9SSylwester Nawrocki }
124814b4dd9SSylwester Nawrocki 
125814b4dd9SSylwester Nawrocki static struct v4l2_mbus_framefmt *__s5k6a3_get_format(
126f7234138SHans Verkuil 		struct s5k6a3 *sensor, struct v4l2_subdev_pad_config *cfg,
127814b4dd9SSylwester Nawrocki 		u32 pad, enum v4l2_subdev_format_whence which)
128814b4dd9SSylwester Nawrocki {
129814b4dd9SSylwester Nawrocki 	if (which == V4L2_SUBDEV_FORMAT_TRY)
130f7234138SHans Verkuil 		return cfg ? v4l2_subdev_get_try_format(&sensor->subdev, cfg, pad) : NULL;
131814b4dd9SSylwester Nawrocki 
132814b4dd9SSylwester Nawrocki 	return &sensor->format;
133814b4dd9SSylwester Nawrocki }
134814b4dd9SSylwester Nawrocki 
135814b4dd9SSylwester Nawrocki static int s5k6a3_set_fmt(struct v4l2_subdev *sd,
136f7234138SHans Verkuil 				  struct v4l2_subdev_pad_config *cfg,
137814b4dd9SSylwester Nawrocki 				  struct v4l2_subdev_format *fmt)
138814b4dd9SSylwester Nawrocki {
139814b4dd9SSylwester Nawrocki 	struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
140814b4dd9SSylwester Nawrocki 	struct v4l2_mbus_framefmt *mf;
141814b4dd9SSylwester Nawrocki 
142814b4dd9SSylwester Nawrocki 	s5k6a3_try_format(&fmt->format);
143814b4dd9SSylwester Nawrocki 
144f7234138SHans Verkuil 	mf = __s5k6a3_get_format(sensor, cfg, fmt->pad, fmt->which);
145814b4dd9SSylwester Nawrocki 	if (mf) {
146814b4dd9SSylwester Nawrocki 		mutex_lock(&sensor->lock);
147814b4dd9SSylwester Nawrocki 		*mf = fmt->format;
148814b4dd9SSylwester Nawrocki 		mutex_unlock(&sensor->lock);
149814b4dd9SSylwester Nawrocki 	}
150814b4dd9SSylwester Nawrocki 	return 0;
151814b4dd9SSylwester Nawrocki }
152814b4dd9SSylwester Nawrocki 
153814b4dd9SSylwester Nawrocki static int s5k6a3_get_fmt(struct v4l2_subdev *sd,
154f7234138SHans Verkuil 			  struct v4l2_subdev_pad_config *cfg,
155814b4dd9SSylwester Nawrocki 			  struct v4l2_subdev_format *fmt)
156814b4dd9SSylwester Nawrocki {
157814b4dd9SSylwester Nawrocki 	struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
158814b4dd9SSylwester Nawrocki 	struct v4l2_mbus_framefmt *mf;
159814b4dd9SSylwester Nawrocki 
160f7234138SHans Verkuil 	mf = __s5k6a3_get_format(sensor, cfg, fmt->pad, fmt->which);
161814b4dd9SSylwester Nawrocki 
162814b4dd9SSylwester Nawrocki 	mutex_lock(&sensor->lock);
163814b4dd9SSylwester Nawrocki 	fmt->format = *mf;
164814b4dd9SSylwester Nawrocki 	mutex_unlock(&sensor->lock);
165814b4dd9SSylwester Nawrocki 	return 0;
166814b4dd9SSylwester Nawrocki }
167814b4dd9SSylwester Nawrocki 
1687c137c60SBhumika Goyal static const struct v4l2_subdev_pad_ops s5k6a3_pad_ops = {
169814b4dd9SSylwester Nawrocki 	.enum_mbus_code	= s5k6a3_enum_mbus_code,
170814b4dd9SSylwester Nawrocki 	.get_fmt	= s5k6a3_get_fmt,
171814b4dd9SSylwester Nawrocki 	.set_fmt	= s5k6a3_set_fmt,
172814b4dd9SSylwester Nawrocki };
173814b4dd9SSylwester Nawrocki 
174814b4dd9SSylwester Nawrocki static int s5k6a3_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
175814b4dd9SSylwester Nawrocki {
176f7234138SHans Verkuil 	struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
177814b4dd9SSylwester Nawrocki 
178814b4dd9SSylwester Nawrocki 	*format		= s5k6a3_formats[0];
179814b4dd9SSylwester Nawrocki 	format->width	= S5K6A3_DEFAULT_WIDTH;
180814b4dd9SSylwester Nawrocki 	format->height	= S5K6A3_DEFAULT_HEIGHT;
181814b4dd9SSylwester Nawrocki 
182814b4dd9SSylwester Nawrocki 	return 0;
183814b4dd9SSylwester Nawrocki }
184814b4dd9SSylwester Nawrocki 
185814b4dd9SSylwester Nawrocki static const struct v4l2_subdev_internal_ops s5k6a3_sd_internal_ops = {
186814b4dd9SSylwester Nawrocki 	.open = s5k6a3_open,
187814b4dd9SSylwester Nawrocki };
188814b4dd9SSylwester Nawrocki 
189814b4dd9SSylwester Nawrocki static int __s5k6a3_power_on(struct s5k6a3 *sensor)
190814b4dd9SSylwester Nawrocki {
191814b4dd9SSylwester Nawrocki 	int i = S5K6A3_SUPP_VDDA;
192814b4dd9SSylwester Nawrocki 	int ret;
193814b4dd9SSylwester Nawrocki 
194814b4dd9SSylwester Nawrocki 	ret = clk_set_rate(sensor->clock, sensor->clock_frequency);
195814b4dd9SSylwester Nawrocki 	if (ret < 0)
196814b4dd9SSylwester Nawrocki 		return ret;
197814b4dd9SSylwester Nawrocki 
198814b4dd9SSylwester Nawrocki 	ret = pm_runtime_get(sensor->dev);
199814b4dd9SSylwester Nawrocki 	if (ret < 0)
200*157282a5SDinghao Liu 		goto error_rpm_put;
201814b4dd9SSylwester Nawrocki 
202814b4dd9SSylwester Nawrocki 	ret = regulator_enable(sensor->supplies[i].consumer);
203814b4dd9SSylwester Nawrocki 	if (ret < 0)
204814b4dd9SSylwester Nawrocki 		goto error_rpm_put;
205814b4dd9SSylwester Nawrocki 
206814b4dd9SSylwester Nawrocki 	ret = clk_prepare_enable(sensor->clock);
207814b4dd9SSylwester Nawrocki 	if (ret < 0)
208814b4dd9SSylwester Nawrocki 		goto error_reg_dis;
209814b4dd9SSylwester Nawrocki 
210814b4dd9SSylwester Nawrocki 	for (i++; i < S5K6A3_NUM_SUPPLIES; i++) {
211814b4dd9SSylwester Nawrocki 		ret = regulator_enable(sensor->supplies[i].consumer);
212814b4dd9SSylwester Nawrocki 		if (ret < 0)
213814b4dd9SSylwester Nawrocki 			goto error_reg_dis;
214814b4dd9SSylwester Nawrocki 	}
215814b4dd9SSylwester Nawrocki 
216814b4dd9SSylwester Nawrocki 	gpio_set_value(sensor->gpio_reset, 1);
217814b4dd9SSylwester Nawrocki 	usleep_range(600, 800);
218814b4dd9SSylwester Nawrocki 	gpio_set_value(sensor->gpio_reset, 0);
219814b4dd9SSylwester Nawrocki 	usleep_range(600, 800);
220814b4dd9SSylwester Nawrocki 	gpio_set_value(sensor->gpio_reset, 1);
221814b4dd9SSylwester Nawrocki 
222814b4dd9SSylwester Nawrocki 	/* Delay needed for the sensor initialization */
223814b4dd9SSylwester Nawrocki 	msleep(20);
224814b4dd9SSylwester Nawrocki 	return 0;
225814b4dd9SSylwester Nawrocki 
226814b4dd9SSylwester Nawrocki error_reg_dis:
227814b4dd9SSylwester Nawrocki 	for (--i; i >= 0; --i)
228814b4dd9SSylwester Nawrocki 		regulator_disable(sensor->supplies[i].consumer);
229814b4dd9SSylwester Nawrocki error_rpm_put:
230814b4dd9SSylwester Nawrocki 	pm_runtime_put(sensor->dev);
231814b4dd9SSylwester Nawrocki 	return ret;
232814b4dd9SSylwester Nawrocki }
233814b4dd9SSylwester Nawrocki 
234814b4dd9SSylwester Nawrocki static int __s5k6a3_power_off(struct s5k6a3 *sensor)
235814b4dd9SSylwester Nawrocki {
236814b4dd9SSylwester Nawrocki 	int i;
237814b4dd9SSylwester Nawrocki 
238814b4dd9SSylwester Nawrocki 	gpio_set_value(sensor->gpio_reset, 0);
239814b4dd9SSylwester Nawrocki 
240814b4dd9SSylwester Nawrocki 	for (i = S5K6A3_NUM_SUPPLIES - 1; i >= 0; i--)
241814b4dd9SSylwester Nawrocki 		regulator_disable(sensor->supplies[i].consumer);
242814b4dd9SSylwester Nawrocki 
243814b4dd9SSylwester Nawrocki 	clk_disable_unprepare(sensor->clock);
244814b4dd9SSylwester Nawrocki 	pm_runtime_put(sensor->dev);
245814b4dd9SSylwester Nawrocki 	return 0;
246814b4dd9SSylwester Nawrocki }
247814b4dd9SSylwester Nawrocki 
248814b4dd9SSylwester Nawrocki static int s5k6a3_s_power(struct v4l2_subdev *sd, int on)
249814b4dd9SSylwester Nawrocki {
250814b4dd9SSylwester Nawrocki 	struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
251814b4dd9SSylwester Nawrocki 	int ret = 0;
252814b4dd9SSylwester Nawrocki 
253814b4dd9SSylwester Nawrocki 	mutex_lock(&sensor->lock);
254814b4dd9SSylwester Nawrocki 
255814b4dd9SSylwester Nawrocki 	if (sensor->power_count == !on) {
256814b4dd9SSylwester Nawrocki 		if (on)
257814b4dd9SSylwester Nawrocki 			ret = __s5k6a3_power_on(sensor);
258814b4dd9SSylwester Nawrocki 		else
259814b4dd9SSylwester Nawrocki 			ret = __s5k6a3_power_off(sensor);
260814b4dd9SSylwester Nawrocki 
261814b4dd9SSylwester Nawrocki 		if (ret == 0)
262814b4dd9SSylwester Nawrocki 			sensor->power_count += on ? 1 : -1;
263814b4dd9SSylwester Nawrocki 	}
264814b4dd9SSylwester Nawrocki 
265814b4dd9SSylwester Nawrocki 	mutex_unlock(&sensor->lock);
266814b4dd9SSylwester Nawrocki 	return ret;
267814b4dd9SSylwester Nawrocki }
268814b4dd9SSylwester Nawrocki 
2697c137c60SBhumika Goyal static const struct v4l2_subdev_core_ops s5k6a3_core_ops = {
270814b4dd9SSylwester Nawrocki 	.s_power = s5k6a3_s_power,
271814b4dd9SSylwester Nawrocki };
272814b4dd9SSylwester Nawrocki 
2737c137c60SBhumika Goyal static const struct v4l2_subdev_ops s5k6a3_subdev_ops = {
274814b4dd9SSylwester Nawrocki 	.core = &s5k6a3_core_ops,
275814b4dd9SSylwester Nawrocki 	.pad = &s5k6a3_pad_ops,
276814b4dd9SSylwester Nawrocki };
277814b4dd9SSylwester Nawrocki 
278e6714993SKieran Bingham static int s5k6a3_probe(struct i2c_client *client)
279814b4dd9SSylwester Nawrocki {
280814b4dd9SSylwester Nawrocki 	struct device *dev = &client->dev;
281814b4dd9SSylwester Nawrocki 	struct s5k6a3 *sensor;
282814b4dd9SSylwester Nawrocki 	struct v4l2_subdev *sd;
283814b4dd9SSylwester Nawrocki 	int gpio, i, ret;
284814b4dd9SSylwester Nawrocki 
285814b4dd9SSylwester Nawrocki 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
286814b4dd9SSylwester Nawrocki 	if (!sensor)
287814b4dd9SSylwester Nawrocki 		return -ENOMEM;
288814b4dd9SSylwester Nawrocki 
289814b4dd9SSylwester Nawrocki 	mutex_init(&sensor->lock);
290814b4dd9SSylwester Nawrocki 	sensor->gpio_reset = -EINVAL;
291814b4dd9SSylwester Nawrocki 	sensor->clock = ERR_PTR(-EINVAL);
292814b4dd9SSylwester Nawrocki 	sensor->dev = dev;
293814b4dd9SSylwester Nawrocki 
294814b4dd9SSylwester Nawrocki 	sensor->clock = devm_clk_get(sensor->dev, S5K6A3_CLK_NAME);
295814b4dd9SSylwester Nawrocki 	if (IS_ERR(sensor->clock))
296814b4dd9SSylwester Nawrocki 		return PTR_ERR(sensor->clock);
297814b4dd9SSylwester Nawrocki 
298814b4dd9SSylwester Nawrocki 	gpio = of_get_gpio_flags(dev->of_node, 0, NULL);
299814b4dd9SSylwester Nawrocki 	if (!gpio_is_valid(gpio))
300814b4dd9SSylwester Nawrocki 		return gpio;
301814b4dd9SSylwester Nawrocki 
302814b4dd9SSylwester Nawrocki 	ret = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_LOW,
303814b4dd9SSylwester Nawrocki 						S5K6A3_DRV_NAME);
304814b4dd9SSylwester Nawrocki 	if (ret < 0)
305814b4dd9SSylwester Nawrocki 		return ret;
306814b4dd9SSylwester Nawrocki 
307814b4dd9SSylwester Nawrocki 	sensor->gpio_reset = gpio;
308814b4dd9SSylwester Nawrocki 
309814b4dd9SSylwester Nawrocki 	if (of_property_read_u32(dev->of_node, "clock-frequency",
310814b4dd9SSylwester Nawrocki 				 &sensor->clock_frequency)) {
311814b4dd9SSylwester Nawrocki 		sensor->clock_frequency = S5K6A3_DEFAULT_CLK_FREQ;
312814b4dd9SSylwester Nawrocki 		dev_info(dev, "using default %u Hz clock frequency\n",
313814b4dd9SSylwester Nawrocki 					sensor->clock_frequency);
314814b4dd9SSylwester Nawrocki 	}
315814b4dd9SSylwester Nawrocki 
316814b4dd9SSylwester Nawrocki 	for (i = 0; i < S5K6A3_NUM_SUPPLIES; i++)
317814b4dd9SSylwester Nawrocki 		sensor->supplies[i].supply = s5k6a3_supply_names[i];
318814b4dd9SSylwester Nawrocki 
319814b4dd9SSylwester Nawrocki 	ret = devm_regulator_bulk_get(&client->dev, S5K6A3_NUM_SUPPLIES,
320814b4dd9SSylwester Nawrocki 				      sensor->supplies);
321814b4dd9SSylwester Nawrocki 	if (ret < 0)
322814b4dd9SSylwester Nawrocki 		return ret;
323814b4dd9SSylwester Nawrocki 
324814b4dd9SSylwester Nawrocki 	sd = &sensor->subdev;
325814b4dd9SSylwester Nawrocki 	v4l2_i2c_subdev_init(sd, client, &s5k6a3_subdev_ops);
326814b4dd9SSylwester Nawrocki 	sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
327814b4dd9SSylwester Nawrocki 	sd->internal_ops = &s5k6a3_sd_internal_ops;
328814b4dd9SSylwester Nawrocki 
329814b4dd9SSylwester Nawrocki 	sensor->format.code = s5k6a3_formats[0].code;
330814b4dd9SSylwester Nawrocki 	sensor->format.width = S5K6A3_DEFAULT_WIDTH;
331814b4dd9SSylwester Nawrocki 	sensor->format.height = S5K6A3_DEFAULT_HEIGHT;
332814b4dd9SSylwester Nawrocki 
333654fa103SSylwester Nawrocki 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
334814b4dd9SSylwester Nawrocki 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
335ab22e77cSMauro Carvalho Chehab 	ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
336814b4dd9SSylwester Nawrocki 	if (ret < 0)
337814b4dd9SSylwester Nawrocki 		return ret;
338814b4dd9SSylwester Nawrocki 
339814b4dd9SSylwester Nawrocki 	pm_runtime_no_callbacks(dev);
340814b4dd9SSylwester Nawrocki 	pm_runtime_enable(dev);
341814b4dd9SSylwester Nawrocki 
342814b4dd9SSylwester Nawrocki 	ret = v4l2_async_register_subdev(sd);
343814b4dd9SSylwester Nawrocki 
344814b4dd9SSylwester Nawrocki 	if (ret < 0) {
345814b4dd9SSylwester Nawrocki 		pm_runtime_disable(&client->dev);
346814b4dd9SSylwester Nawrocki 		media_entity_cleanup(&sd->entity);
347814b4dd9SSylwester Nawrocki 	}
348814b4dd9SSylwester Nawrocki 
349814b4dd9SSylwester Nawrocki 	return ret;
350814b4dd9SSylwester Nawrocki }
351814b4dd9SSylwester Nawrocki 
352814b4dd9SSylwester Nawrocki static int s5k6a3_remove(struct i2c_client *client)
353814b4dd9SSylwester Nawrocki {
354814b4dd9SSylwester Nawrocki 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
355814b4dd9SSylwester Nawrocki 
356814b4dd9SSylwester Nawrocki 	pm_runtime_disable(&client->dev);
357814b4dd9SSylwester Nawrocki 	v4l2_async_unregister_subdev(sd);
358814b4dd9SSylwester Nawrocki 	media_entity_cleanup(&sd->entity);
359814b4dd9SSylwester Nawrocki 	return 0;
360814b4dd9SSylwester Nawrocki }
361814b4dd9SSylwester Nawrocki 
362814b4dd9SSylwester Nawrocki static const struct i2c_device_id s5k6a3_ids[] = {
363814b4dd9SSylwester Nawrocki 	{ }
364814b4dd9SSylwester Nawrocki };
365ed8d1cf0SJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, s5k6a3_ids);
366814b4dd9SSylwester Nawrocki 
367814b4dd9SSylwester Nawrocki #ifdef CONFIG_OF
368814b4dd9SSylwester Nawrocki static const struct of_device_id s5k6a3_of_match[] = {
369814b4dd9SSylwester Nawrocki 	{ .compatible = "samsung,s5k6a3" },
370814b4dd9SSylwester Nawrocki 	{ /* sentinel */ }
371814b4dd9SSylwester Nawrocki };
372814b4dd9SSylwester Nawrocki MODULE_DEVICE_TABLE(of, s5k6a3_of_match);
373814b4dd9SSylwester Nawrocki #endif
374814b4dd9SSylwester Nawrocki 
375814b4dd9SSylwester Nawrocki static struct i2c_driver s5k6a3_driver = {
376814b4dd9SSylwester Nawrocki 	.driver = {
377814b4dd9SSylwester Nawrocki 		.of_match_table	= of_match_ptr(s5k6a3_of_match),
378814b4dd9SSylwester Nawrocki 		.name		= S5K6A3_DRV_NAME,
379814b4dd9SSylwester Nawrocki 	},
380e6714993SKieran Bingham 	.probe_new	= s5k6a3_probe,
381814b4dd9SSylwester Nawrocki 	.remove		= s5k6a3_remove,
382814b4dd9SSylwester Nawrocki 	.id_table	= s5k6a3_ids,
383814b4dd9SSylwester Nawrocki };
384814b4dd9SSylwester Nawrocki 
385814b4dd9SSylwester Nawrocki module_i2c_driver(s5k6a3_driver);
386814b4dd9SSylwester Nawrocki 
387814b4dd9SSylwester Nawrocki MODULE_DESCRIPTION("S5K6A3 image sensor subdev driver");
388814b4dd9SSylwester Nawrocki MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
389814b4dd9SSylwester Nawrocki MODULE_LICENSE("GPL v2");
390