1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * adv7181 driver
4 *
5 * Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd.
6 */
7
8 #include <linux/clk.h>
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/sysfs.h>
16 #include <media/media-entity.h>
17 #include <media/v4l2-async.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-subdev.h>
20
21 #define REG_CHIP_ID 0x11
22 #define CHIP_ID 0x20
23
24 #define REG_SC_CTRL_MODE 0x03
25 #define SC_CTRL_MODE_STANDBY 0x4c
26 #define SC_CTRL_MODE_STREAMING 0x0c
27
28 #define REG_NULL 0xFF
29
30 #define ADV7181_XVCLK_FREQ 24000000
31 #define ADV7181_LANES 1
32 #define ADV7181_BITS_PER_SAMPLE 10
33
34 #define ADV7181_SKIP_TOP 24
35
36 static const char * const adv7181_supply_names[] = {
37 "dvdd",
38 "dvddio",
39 };
40
41 #define ADV7181_NUM_SUPPLIES ARRAY_SIZE(adv7181_supply_names)
42
43 struct regval {
44 u8 addr;
45 u8 val;
46 };
47
48 struct adv7181_mode {
49 u32 width;
50 u32 height;
51 const struct regval *reg_list;
52 };
53
54 struct adv7181 {
55 struct i2c_client *client;
56 struct clk *xvclk;
57 struct gpio_desc *reset_gpio;
58 struct regulator_bulk_data supplies[ADV7181_NUM_SUPPLIES];
59
60 bool streaming;
61 struct mutex mutex; /* lock to serialize v4l2 callback */
62 struct v4l2_subdev subdev;
63 struct media_pad pad;
64
65 int skip_top;
66
67 const struct adv7181_mode *cur_mode;
68 };
69
70 #define to_adv7181(sd) container_of(sd, struct adv7181, subdev)
71
72 /* PLL settings bases on 28M xvclk, resolution 720x480 30fps*/
73 static struct regval adv7181_cvbs_30fps[] = {
74 {0x00, 0x0B},
75 {0x04, 0x77},
76 {0x17, 0x41},
77 {0x1D, 0x47},
78 {0x31, 0x02},
79 {0x3A, 0x17},
80 {0x3B, 0x81},
81 {0x3D, 0xA2},
82 {0x3E, 0x6A},
83 {0x3F, 0xA0},
84 {0x86, 0x0B},
85 {0xF3, 0x01},
86 {0xF9, 0x03},
87 {0x0E, 0x80},
88 {0x52, 0x46},
89 {0x54, 0x80},
90 {0x7F, 0xFF},
91 {0x81, 0x30},
92 {0x90, 0xC9},
93 {0x91, 0x40},
94 {0x92, 0x3C},
95 {0x93, 0xCA},
96 {0x94, 0xD5},
97 {0xB1, 0xFF},
98 {0xB6, 0x08},
99 {0xC0, 0x9A},
100 {0xCF, 0x50},
101 {0xD0, 0x4E},
102 {0xD1, 0xB9},
103 {0xD6, 0xDD},
104 {0xD7, 0xE2},
105 {0xE5, 0x51},
106 {0xF6, 0x3B},
107 {0x0E, 0x00},
108 {0x03, 0x4C},
109 {REG_NULL, 0x0},
110 };
111
112 static const struct adv7181_mode supported_modes[] = {
113 {
114 .width = 720,
115 .height = 480,
116 .reg_list = adv7181_cvbs_30fps,
117 },
118 };
119
adv7181_write_reg(struct i2c_client * client,u8 reg,u8 val)120 static int adv7181_write_reg(struct i2c_client *client, u8 reg, u8 val)
121 {
122 int ret;
123
124 ret = i2c_smbus_write_byte_data(client, reg, val);
125
126 if (ret < 0)
127 dev_err(&client->dev, "write reg error: %d\n", ret);
128
129 return ret;
130 }
131
adv7181_write_array(struct i2c_client * client,const struct regval * regs)132 static int adv7181_write_array(struct i2c_client *client,
133 const struct regval *regs)
134 {
135 int i, ret = 0;
136
137 for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
138 ret = adv7181_write_reg(client, regs[i].addr, regs[i].val);
139
140 return ret;
141 }
142
adv7181_read_reg(struct i2c_client * client,u8 reg)143 static inline u8 adv7181_read_reg(struct i2c_client *client, u8 reg)
144 {
145 return i2c_smbus_read_byte_data(client, reg);
146 }
147
adv7181_fill_fmt(const struct adv7181_mode * mode,struct v4l2_mbus_framefmt * fmt)148 static void adv7181_fill_fmt(const struct adv7181_mode *mode,
149 struct v4l2_mbus_framefmt *fmt)
150 {
151 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
152 fmt->width = mode->width;
153 fmt->height = mode->height;
154 fmt->field = V4L2_FIELD_NONE;
155 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
156 }
157
adv7181_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)158 static int adv7181_set_fmt(struct v4l2_subdev *sd,
159 struct v4l2_subdev_pad_config *cfg,
160 struct v4l2_subdev_format *fmt)
161 {
162 struct adv7181 *adv7181 = to_adv7181(sd);
163 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
164
165 /* only one mode supported for now */
166 adv7181_fill_fmt(adv7181->cur_mode, mbus_fmt);
167
168 return 0;
169 }
170
adv7181_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)171 static int adv7181_get_fmt(struct v4l2_subdev *sd,
172 struct v4l2_subdev_pad_config *cfg,
173 struct v4l2_subdev_format *fmt)
174 {
175 struct adv7181 *adv7181 = to_adv7181(sd);
176 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
177
178 adv7181_fill_fmt(adv7181->cur_mode, mbus_fmt);
179
180 return 0;
181 }
182
adv7181_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)183 static int adv7181_enum_mbus_code(struct v4l2_subdev *sd,
184 struct v4l2_subdev_pad_config *cfg,
185 struct v4l2_subdev_mbus_code_enum *code)
186 {
187 if (code->index >= ARRAY_SIZE(supported_modes))
188 return -EINVAL;
189
190 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
191
192 return 0;
193 }
194
adv7181_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)195 static int adv7181_enum_frame_sizes(struct v4l2_subdev *sd,
196 struct v4l2_subdev_pad_config *cfg,
197 struct v4l2_subdev_frame_size_enum *fse)
198 {
199 u32 index = fse->index;
200
201 if (index >= ARRAY_SIZE(supported_modes))
202 return -EINVAL;
203
204 fse->code = MEDIA_BUS_FMT_UYVY8_2X8;
205
206 fse->min_width = supported_modes[index].width;
207 fse->max_width = supported_modes[index].width;
208 fse->max_height = supported_modes[index].height;
209 fse->min_height = supported_modes[index].height;
210
211 return 0;
212 }
213
adv7181_g_skip_top_lines(struct v4l2_subdev * sd,u32 * lines)214 static int adv7181_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
215 {
216 struct adv7181 *adv7181 = to_adv7181(sd);
217
218 *lines = adv7181->skip_top;
219
220 return 0;
221 }
222
adv7181_querystd(struct v4l2_subdev * sd,v4l2_std_id * std)223 static int adv7181_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
224 {
225 /* Only NTSC now */
226 *std = V4L2_STD_NTSC;
227
228 return 0;
229 }
230
__adv7181_power_on(struct adv7181 * adv7181)231 static int __adv7181_power_on(struct adv7181 *adv7181)
232 {
233 int ret;
234 struct device *dev = &adv7181->client->dev;
235
236 if (!IS_ERR(adv7181->xvclk)) {
237 ret = clk_prepare_enable(adv7181->xvclk);
238 if (ret < 0) {
239 dev_err(dev, "Failed to enable xvclk\n");
240 return ret;
241 }
242 }
243
244 gpiod_set_value_cansleep(adv7181->reset_gpio, 1);
245
246 ret = regulator_bulk_enable(ADV7181_NUM_SUPPLIES, adv7181->supplies);
247 if (ret < 0) {
248 dev_err(dev, "Failed to enable regulators\n");
249 goto disable_clk;
250 }
251
252 gpiod_set_value_cansleep(adv7181->reset_gpio, 0);
253
254 return 0;
255
256 disable_clk:
257 if (!IS_ERR(adv7181->xvclk))
258 clk_disable_unprepare(adv7181->xvclk);
259
260 return ret;
261 }
262
__adv7181_power_off(struct adv7181 * adv7181)263 static void __adv7181_power_off(struct adv7181 *adv7181)
264 {
265 if (!IS_ERR(adv7181->xvclk))
266 clk_disable_unprepare(adv7181->xvclk);
267 gpiod_set_value_cansleep(adv7181->reset_gpio, 1);
268 regulator_bulk_disable(ADV7181_NUM_SUPPLIES, adv7181->supplies);
269 }
270
adv7181_s_stream(struct v4l2_subdev * sd,int on)271 static int adv7181_s_stream(struct v4l2_subdev *sd, int on)
272 {
273 struct adv7181 *adv7181 = to_adv7181(sd);
274 struct i2c_client *client = adv7181->client;
275 int ret = 0;
276
277 mutex_lock(&adv7181->mutex);
278
279 on = !!on;
280 if (on == adv7181->streaming)
281 goto unlock_and_return;
282
283 if (on) {
284 ret = pm_runtime_get_sync(&adv7181->client->dev);
285 if (ret < 0) {
286 pm_runtime_put_noidle(&client->dev);
287 goto unlock_and_return;
288 }
289
290 ret = adv7181_write_array(adv7181->client,
291 adv7181->cur_mode->reg_list);
292 if (ret) {
293 pm_runtime_put(&client->dev);
294 goto unlock_and_return;
295 }
296
297 ret = adv7181_write_reg(client, REG_SC_CTRL_MODE,
298 SC_CTRL_MODE_STREAMING);
299 if (ret) {
300 pm_runtime_put(&client->dev);
301 goto unlock_and_return;
302 }
303 } else {
304 adv7181_write_reg(client, REG_SC_CTRL_MODE,
305 SC_CTRL_MODE_STANDBY);
306 pm_runtime_put(&client->dev);
307 }
308
309 adv7181->streaming = on;
310
311 unlock_and_return:
312 mutex_unlock(&adv7181->mutex);
313
314 return ret;
315 }
316
317 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
adv7181_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)318 static int adv7181_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
319 {
320 struct adv7181 *adv7181 = to_adv7181(sd);
321 struct v4l2_mbus_framefmt *try_fmt;
322
323 mutex_lock(&adv7181->mutex);
324
325 try_fmt = v4l2_subdev_get_try_format(sd, fh->pad, 0);
326 /* Initialize try_fmt */
327 adv7181_fill_fmt(&supported_modes[0], try_fmt);
328
329 mutex_unlock(&adv7181->mutex);
330
331 return 0;
332 }
333 #endif
334
adv7181_runtime_resume(struct device * dev)335 static int adv7181_runtime_resume(struct device *dev)
336 {
337 struct i2c_client *client = to_i2c_client(dev);
338 struct v4l2_subdev *sd = i2c_get_clientdata(client);
339 struct adv7181 *adv7181 = to_adv7181(sd);
340
341 return __adv7181_power_on(adv7181);
342 }
343
adv7181_runtime_suspend(struct device * dev)344 static int adv7181_runtime_suspend(struct device *dev)
345 {
346 struct i2c_client *client = to_i2c_client(dev);
347 struct v4l2_subdev *sd = i2c_get_clientdata(client);
348 struct adv7181 *adv7181 = to_adv7181(sd);
349
350 __adv7181_power_off(adv7181);
351
352 return 0;
353 }
354
355 static const struct dev_pm_ops adv7181_pm_ops = {
356 SET_RUNTIME_PM_OPS(adv7181_runtime_suspend,
357 adv7181_runtime_resume, NULL)
358 };
359
360 static const struct v4l2_subdev_video_ops adv7181_video_ops = {
361 .s_stream = adv7181_s_stream,
362 .querystd = adv7181_querystd,
363 };
364
365 static const struct v4l2_subdev_pad_ops adv7181_pad_ops = {
366 .enum_mbus_code = adv7181_enum_mbus_code,
367 .enum_frame_size = adv7181_enum_frame_sizes,
368 .get_fmt = adv7181_get_fmt,
369 .set_fmt = adv7181_set_fmt,
370 };
371
372 static struct v4l2_subdev_sensor_ops adv7181_sensor_ops = {
373 .g_skip_top_lines = adv7181_g_skip_top_lines,
374 };
375
376 static const struct v4l2_subdev_ops adv7181_subdev_ops = {
377 .video = &adv7181_video_ops,
378 .pad = &adv7181_pad_ops,
379 .sensor = &adv7181_sensor_ops,
380 };
381
382 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
383 static const struct v4l2_subdev_internal_ops adv7181_internal_ops = {
384 .open = adv7181_open,
385 };
386 #endif
387
adv7181_check_sensor_id(struct adv7181 * adv7181,struct i2c_client * client)388 static int adv7181_check_sensor_id(struct adv7181 *adv7181,
389 struct i2c_client *client)
390 {
391 struct device *dev = &adv7181->client->dev;
392 u8 id;
393
394 id = adv7181_read_reg(client, REG_CHIP_ID);
395
396 if (id != CHIP_ID) {
397 dev_err(dev, "Wrong camera sensor id(%04x)\n", id);
398 return -EINVAL;
399 }
400
401 dev_info(dev, "Detected ADV7181 (%04x) sensor\n", CHIP_ID);
402
403 return 0;
404 }
405
adv7181_configure_regulators(struct adv7181 * adv7181)406 static int adv7181_configure_regulators(struct adv7181 *adv7181)
407 {
408 u32 i;
409
410 for (i = 0; i < ADV7181_NUM_SUPPLIES; i++)
411 adv7181->supplies[i].supply = adv7181_supply_names[i];
412
413 return devm_regulator_bulk_get(&adv7181->client->dev,
414 ADV7181_NUM_SUPPLIES,
415 adv7181->supplies);
416 }
417
adv7181_probe(struct i2c_client * client,const struct i2c_device_id * id)418 static int adv7181_probe(struct i2c_client *client,
419 const struct i2c_device_id *id)
420 {
421 struct device *dev = &client->dev;
422 struct adv7181 *adv7181;
423 int ret;
424
425 adv7181 = devm_kzalloc(dev, sizeof(*adv7181), GFP_KERNEL);
426 if (!adv7181)
427 return -ENOMEM;
428
429 adv7181->skip_top = ADV7181_SKIP_TOP;
430
431 adv7181->client = client;
432 adv7181->cur_mode = &supported_modes[0];
433
434 adv7181->xvclk = devm_clk_get(dev, "xvclk");
435 if (!IS_ERR(adv7181->xvclk)) {
436 ret = clk_set_rate(adv7181->xvclk, ADV7181_XVCLK_FREQ);
437 if (ret < 0) {
438 dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
439 return ret;
440 }
441 if (clk_get_rate(adv7181->xvclk) != ADV7181_XVCLK_FREQ)
442 dev_warn(dev, "xvclk mismatched, it requires 24MHz\n");
443 }
444
445 adv7181->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
446 if (IS_ERR(adv7181->reset_gpio)) {
447 dev_err(dev, "Failed to get reset-gpios\n");
448 return -EINVAL;
449 }
450
451 ret = adv7181_configure_regulators(adv7181);
452 if (ret) {
453 dev_err(dev, "Failed to get power regulators\n");
454 return ret;
455 }
456
457 mutex_init(&adv7181->mutex);
458 v4l2_i2c_subdev_init(&adv7181->subdev, client, &adv7181_subdev_ops);
459
460 ret = __adv7181_power_on(adv7181);
461 if (ret)
462 goto err_destroy_mutex;
463
464 ret = adv7181_check_sensor_id(adv7181, client);
465 if (ret)
466 goto err_power_off;
467
468 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
469 adv7181->subdev.internal_ops = &adv7181_internal_ops;
470 adv7181->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
471 #endif
472 #if defined(CONFIG_MEDIA_CONTROLLER)
473 adv7181->pad.flags = MEDIA_PAD_FL_SOURCE;
474 adv7181->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
475 ret = media_entity_init(&adv7181->subdev.entity, 1, &adv7181->pad, 0);
476 if (ret < 0)
477 goto err_power_off;
478 #endif
479
480 ret = v4l2_async_register_subdev(&adv7181->subdev);
481 if (ret) {
482 dev_err(dev, "v4l2 async register subdev failed\n");
483 goto err_clean_entity;
484 }
485
486 pm_runtime_set_active(dev);
487 pm_runtime_enable(dev);
488 pm_runtime_idle(dev);
489
490 return 0;
491
492 err_clean_entity:
493 #if defined(CONFIG_MEDIA_CONTROLLER)
494 media_entity_cleanup(&adv7181->subdev.entity);
495 #endif
496 err_power_off:
497 __adv7181_power_off(adv7181);
498 err_destroy_mutex:
499 mutex_destroy(&adv7181->mutex);
500
501 return ret;
502 }
503
adv7181_remove(struct i2c_client * client)504 static int adv7181_remove(struct i2c_client *client)
505 {
506 struct v4l2_subdev *sd = i2c_get_clientdata(client);
507 struct adv7181 *adv7181 = to_adv7181(sd);
508
509 v4l2_async_unregister_subdev(sd);
510 #if defined(CONFIG_MEDIA_CONTROLLER)
511 media_entity_cleanup(&sd->entity);
512 #endif
513 mutex_destroy(&adv7181->mutex);
514
515 pm_runtime_disable(&client->dev);
516 if (!pm_runtime_status_suspended(&client->dev))
517 __adv7181_power_off(adv7181);
518 pm_runtime_set_suspended(&client->dev);
519
520 return 0;
521 }
522
523 static const struct i2c_device_id adv7181_id[] = {
524 {"adv7181", 0},
525 {},
526 };
527
528 #if IS_ENABLED(CONFIG_OF)
529 static const struct of_device_id adv7181_of_match[] = {
530 { .compatible = "adi,adv7181" },
531 {},
532 };
533 MODULE_DEVICE_TABLE(of, adv7181_of_match);
534 #endif
535
536 static struct i2c_driver adv7181_i2c_driver = {
537 .driver = {
538 .name = "adv7181",
539 .pm = &adv7181_pm_ops,
540 .of_match_table = adv7181_of_match
541 },
542 .probe = adv7181_probe,
543 .remove = adv7181_remove,
544 .id_table = adv7181_id,
545 };
546
547 module_i2c_driver(adv7181_i2c_driver);
548
549 MODULE_DESCRIPTION("adv7181 sensor driver");
550 MODULE_LICENSE("GPL v2");
551