Merge 5.0-rc6 into driver-core-next
[linux/fpc-iii.git] / drivers / media / i2c / et8ek8 / et8ek8_driver.c
blob37ef38947e018619978097e296e50eac8d0034ad
1 /*
2 * et8ek8_driver.c
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
7 * Tuukka Toivonen <tuukkat76@gmail.com>
8 * Pavel Machek <pavel@ucw.cz>
10 * Based on code from Toni Leinonen <toni.leinonen@offcode.fi>.
12 * This driver is based on the Micron MT9T012 camera imager driver
13 * (C) Texas Instruments.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
25 #include <linux/clk.h>
26 #include <linux/delay.h>
27 #include <linux/gpio/consumer.h>
28 #include <linux/i2c.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/slab.h>
34 #include <linux/sort.h>
35 #include <linux/v4l2-mediabus.h>
37 #include <media/media-entity.h>
38 #include <media/v4l2-ctrls.h>
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-subdev.h>
42 #include "et8ek8_reg.h"
44 #define ET8EK8_NAME "et8ek8"
45 #define ET8EK8_PRIV_MEM_SIZE 128
46 #define ET8EK8_MAX_MSG 8
48 struct et8ek8_sensor {
49 struct v4l2_subdev subdev;
50 struct media_pad pad;
51 struct v4l2_mbus_framefmt format;
52 struct gpio_desc *reset;
53 struct regulator *vana;
54 struct clk *ext_clk;
55 u32 xclk_freq;
57 u16 version;
59 struct v4l2_ctrl_handler ctrl_handler;
60 struct v4l2_ctrl *exposure;
61 struct v4l2_ctrl *pixel_rate;
62 struct et8ek8_reglist *current_reglist;
64 u8 priv_mem[ET8EK8_PRIV_MEM_SIZE];
66 struct mutex power_lock;
67 int power_count;
70 #define to_et8ek8_sensor(sd) container_of(sd, struct et8ek8_sensor, subdev)
72 enum et8ek8_versions {
73 ET8EK8_REV_1 = 0x0001,
74 ET8EK8_REV_2,
78 * This table describes what should be written to the sensor register
79 * for each gain value. The gain(index in the table) is in terms of
80 * 0.1EV, i.e. 10 indexes in the table give 2 time more gain [0] in
81 * the *analog gain, [1] in the digital gain
83 * Analog gain [dB] = 20*log10(regvalue/32); 0x20..0x100
85 static struct et8ek8_gain {
86 u16 analog;
87 u16 digital;
88 } const et8ek8_gain_table[] = {
89 { 32, 0}, /* x1 */
90 { 34, 0},
91 { 37, 0},
92 { 39, 0},
93 { 42, 0},
94 { 45, 0},
95 { 49, 0},
96 { 52, 0},
97 { 56, 0},
98 { 60, 0},
99 { 64, 0}, /* x2 */
100 { 69, 0},
101 { 74, 0},
102 { 79, 0},
103 { 84, 0},
104 { 91, 0},
105 { 97, 0},
106 {104, 0},
107 {111, 0},
108 {119, 0},
109 {128, 0}, /* x4 */
110 {137, 0},
111 {147, 0},
112 {158, 0},
113 {169, 0},
114 {181, 0},
115 {194, 0},
116 {208, 0},
117 {223, 0},
118 {239, 0},
119 {256, 0}, /* x8 */
120 {256, 73},
121 {256, 152},
122 {256, 236},
123 {256, 327},
124 {256, 424},
125 {256, 528},
126 {256, 639},
127 {256, 758},
128 {256, 886},
129 {256, 1023}, /* x16 */
132 /* Register definitions */
133 #define REG_REVISION_NUMBER_L 0x1200
134 #define REG_REVISION_NUMBER_H 0x1201
136 #define PRIV_MEM_START_REG 0x0008
137 #define PRIV_MEM_WIN_SIZE 8
139 #define ET8EK8_I2C_DELAY 3 /* msec delay b/w accesses */
141 #define USE_CRC 1
144 * Register access helpers
146 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
147 * Returns zero if successful, or non-zero otherwise.
149 static int et8ek8_i2c_read_reg(struct i2c_client *client, u16 data_length,
150 u16 reg, u32 *val)
152 int r;
153 struct i2c_msg msg;
154 unsigned char data[4];
156 if (!client->adapter)
157 return -ENODEV;
158 if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT)
159 return -EINVAL;
161 msg.addr = client->addr;
162 msg.flags = 0;
163 msg.len = 2;
164 msg.buf = data;
166 /* high byte goes out first */
167 data[0] = (u8) (reg >> 8);
168 data[1] = (u8) (reg & 0xff);
169 r = i2c_transfer(client->adapter, &msg, 1);
170 if (r < 0)
171 goto err;
173 msg.len = data_length;
174 msg.flags = I2C_M_RD;
175 r = i2c_transfer(client->adapter, &msg, 1);
176 if (r < 0)
177 goto err;
179 *val = 0;
180 /* high byte comes first */
181 if (data_length == ET8EK8_REG_8BIT)
182 *val = data[0];
183 else
184 *val = (data[1] << 8) + data[0];
186 return 0;
188 err:
189 dev_err(&client->dev, "read from offset 0x%x error %d\n", reg, r);
191 return r;
194 static void et8ek8_i2c_create_msg(struct i2c_client *client, u16 len, u16 reg,
195 u32 val, struct i2c_msg *msg,
196 unsigned char *buf)
198 msg->addr = client->addr;
199 msg->flags = 0; /* Write */
200 msg->len = 2 + len;
201 msg->buf = buf;
203 /* high byte goes out first */
204 buf[0] = (u8) (reg >> 8);
205 buf[1] = (u8) (reg & 0xff);
207 switch (len) {
208 case ET8EK8_REG_8BIT:
209 buf[2] = (u8) (val) & 0xff;
210 break;
211 case ET8EK8_REG_16BIT:
212 buf[2] = (u8) (val) & 0xff;
213 buf[3] = (u8) (val >> 8) & 0xff;
214 break;
215 default:
216 WARN_ONCE(1, ET8EK8_NAME ": %s: invalid message length.\n",
217 __func__);
222 * A buffered write method that puts the wanted register write
223 * commands in smaller number of message lists and passes the lists to
224 * the i2c framework
226 static int et8ek8_i2c_buffered_write_regs(struct i2c_client *client,
227 const struct et8ek8_reg *wnext,
228 int cnt)
230 struct i2c_msg msg[ET8EK8_MAX_MSG];
231 unsigned char data[ET8EK8_MAX_MSG][6];
232 int wcnt = 0;
233 u16 reg, data_length;
234 u32 val;
235 int rval;
237 /* Create new write messages for all writes */
238 while (wcnt < cnt) {
239 data_length = wnext->type;
240 reg = wnext->reg;
241 val = wnext->val;
242 wnext++;
244 et8ek8_i2c_create_msg(client, data_length, reg,
245 val, &msg[wcnt], &data[wcnt][0]);
247 /* Update write count */
248 wcnt++;
250 if (wcnt < ET8EK8_MAX_MSG)
251 continue;
253 rval = i2c_transfer(client->adapter, msg, wcnt);
254 if (rval < 0)
255 return rval;
257 cnt -= wcnt;
258 wcnt = 0;
261 rval = i2c_transfer(client->adapter, msg, wcnt);
263 return rval < 0 ? rval : 0;
267 * Write a list of registers to i2c device.
269 * The list of registers is terminated by ET8EK8_REG_TERM.
270 * Returns zero if successful, or non-zero otherwise.
272 static int et8ek8_i2c_write_regs(struct i2c_client *client,
273 const struct et8ek8_reg *regs)
275 int r, cnt = 0;
276 const struct et8ek8_reg *next;
278 if (!client->adapter)
279 return -ENODEV;
281 if (!regs)
282 return -EINVAL;
284 /* Initialize list pointers to the start of the list */
285 next = regs;
287 do {
289 * We have to go through the list to figure out how
290 * many regular writes we have in a row
292 while (next->type != ET8EK8_REG_TERM &&
293 next->type != ET8EK8_REG_DELAY) {
295 * Here we check that the actual length fields
296 * are valid
298 if (WARN(next->type != ET8EK8_REG_8BIT &&
299 next->type != ET8EK8_REG_16BIT,
300 "Invalid type = %d", next->type)) {
301 return -EINVAL;
304 * Increment count of successive writes and
305 * read pointer
307 cnt++;
308 next++;
311 /* Now we start writing ... */
312 r = et8ek8_i2c_buffered_write_regs(client, regs, cnt);
314 /* ... and then check that everything was OK */
315 if (r < 0) {
316 dev_err(&client->dev, "i2c transfer error!\n");
317 return r;
321 * If we ran into a sleep statement when going through
322 * the list, this is where we snooze for the required time
324 if (next->type == ET8EK8_REG_DELAY) {
325 msleep(next->val);
327 * ZZZ ...
328 * Update list pointers and cnt and start over ...
330 next++;
331 regs = next;
332 cnt = 0;
334 } while (next->type != ET8EK8_REG_TERM);
336 return 0;
340 * Write to a 8/16-bit register.
341 * Returns zero if successful, or non-zero otherwise.
343 static int et8ek8_i2c_write_reg(struct i2c_client *client, u16 data_length,
344 u16 reg, u32 val)
346 int r;
347 struct i2c_msg msg;
348 unsigned char data[6];
350 if (!client->adapter)
351 return -ENODEV;
352 if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT)
353 return -EINVAL;
355 et8ek8_i2c_create_msg(client, data_length, reg, val, &msg, data);
357 r = i2c_transfer(client->adapter, &msg, 1);
358 if (r < 0) {
359 dev_err(&client->dev,
360 "wrote 0x%x to offset 0x%x error %d\n", val, reg, r);
361 return r;
364 return 0;
367 static struct et8ek8_reglist *et8ek8_reglist_find_type(
368 struct et8ek8_meta_reglist *meta,
369 u16 type)
371 struct et8ek8_reglist **next = &meta->reglist[0].ptr;
373 while (*next) {
374 if ((*next)->type == type)
375 return *next;
377 next++;
380 return NULL;
383 static int et8ek8_i2c_reglist_find_write(struct i2c_client *client,
384 struct et8ek8_meta_reglist *meta,
385 u16 type)
387 struct et8ek8_reglist *reglist;
389 reglist = et8ek8_reglist_find_type(meta, type);
390 if (!reglist)
391 return -EINVAL;
393 return et8ek8_i2c_write_regs(client, reglist->regs);
396 static struct et8ek8_reglist **et8ek8_reglist_first(
397 struct et8ek8_meta_reglist *meta)
399 return &meta->reglist[0].ptr;
402 static void et8ek8_reglist_to_mbus(const struct et8ek8_reglist *reglist,
403 struct v4l2_mbus_framefmt *fmt)
405 fmt->width = reglist->mode.window_width;
406 fmt->height = reglist->mode.window_height;
407 fmt->code = reglist->mode.bus_format;
410 static struct et8ek8_reglist *et8ek8_reglist_find_mode_fmt(
411 struct et8ek8_meta_reglist *meta,
412 struct v4l2_mbus_framefmt *fmt)
414 struct et8ek8_reglist **list = et8ek8_reglist_first(meta);
415 struct et8ek8_reglist *best_match = NULL;
416 struct et8ek8_reglist *best_other = NULL;
417 struct v4l2_mbus_framefmt format;
418 unsigned int max_dist_match = (unsigned int)-1;
419 unsigned int max_dist_other = (unsigned int)-1;
422 * Find the mode with the closest image size. The distance between
423 * image sizes is the size in pixels of the non-overlapping regions
424 * between the requested size and the frame-specified size.
426 * Store both the closest mode that matches the requested format, and
427 * the closest mode for all other formats. The best match is returned
428 * if found, otherwise the best mode with a non-matching format is
429 * returned.
431 for (; *list; list++) {
432 unsigned int dist;
434 if ((*list)->type != ET8EK8_REGLIST_MODE)
435 continue;
437 et8ek8_reglist_to_mbus(*list, &format);
439 dist = min(fmt->width, format.width)
440 * min(fmt->height, format.height);
441 dist = format.width * format.height
442 + fmt->width * fmt->height - 2 * dist;
445 if (fmt->code == format.code) {
446 if (dist < max_dist_match || !best_match) {
447 best_match = *list;
448 max_dist_match = dist;
450 } else {
451 if (dist < max_dist_other || !best_other) {
452 best_other = *list;
453 max_dist_other = dist;
458 return best_match ? best_match : best_other;
461 #define TIMEPERFRAME_AVG_FPS(t) \
462 (((t).denominator + ((t).numerator >> 1)) / (t).numerator)
464 static struct et8ek8_reglist *et8ek8_reglist_find_mode_ival(
465 struct et8ek8_meta_reglist *meta,
466 struct et8ek8_reglist *current_reglist,
467 struct v4l2_fract *timeperframe)
469 int fps = TIMEPERFRAME_AVG_FPS(*timeperframe);
470 struct et8ek8_reglist **list = et8ek8_reglist_first(meta);
471 struct et8ek8_mode *current_mode = &current_reglist->mode;
473 for (; *list; list++) {
474 struct et8ek8_mode *mode = &(*list)->mode;
476 if ((*list)->type != ET8EK8_REGLIST_MODE)
477 continue;
479 if (mode->window_width != current_mode->window_width ||
480 mode->window_height != current_mode->window_height)
481 continue;
483 if (TIMEPERFRAME_AVG_FPS(mode->timeperframe) == fps)
484 return *list;
487 return NULL;
490 static int et8ek8_reglist_cmp(const void *a, const void *b)
492 const struct et8ek8_reglist **list1 = (const struct et8ek8_reglist **)a,
493 **list2 = (const struct et8ek8_reglist **)b;
495 /* Put real modes in the beginning. */
496 if ((*list1)->type == ET8EK8_REGLIST_MODE &&
497 (*list2)->type != ET8EK8_REGLIST_MODE)
498 return -1;
499 if ((*list1)->type != ET8EK8_REGLIST_MODE &&
500 (*list2)->type == ET8EK8_REGLIST_MODE)
501 return 1;
503 /* Descending width. */
504 if ((*list1)->mode.window_width > (*list2)->mode.window_width)
505 return -1;
506 if ((*list1)->mode.window_width < (*list2)->mode.window_width)
507 return 1;
509 if ((*list1)->mode.window_height > (*list2)->mode.window_height)
510 return -1;
511 if ((*list1)->mode.window_height < (*list2)->mode.window_height)
512 return 1;
514 return 0;
517 static int et8ek8_reglist_import(struct i2c_client *client,
518 struct et8ek8_meta_reglist *meta)
520 int nlists = 0, i;
522 dev_info(&client->dev, "meta_reglist version %s\n", meta->version);
524 while (meta->reglist[nlists].ptr)
525 nlists++;
527 if (!nlists)
528 return -EINVAL;
530 sort(&meta->reglist[0].ptr, nlists, sizeof(meta->reglist[0].ptr),
531 et8ek8_reglist_cmp, NULL);
533 i = nlists;
534 nlists = 0;
536 while (i--) {
537 struct et8ek8_reglist *list;
539 list = meta->reglist[nlists].ptr;
541 dev_dbg(&client->dev,
542 "%s: type %d\tw %d\th %d\tfmt %x\tival %d/%d\tptr %p\n",
543 __func__,
544 list->type,
545 list->mode.window_width, list->mode.window_height,
546 list->mode.bus_format,
547 list->mode.timeperframe.numerator,
548 list->mode.timeperframe.denominator,
549 (void *)meta->reglist[nlists].ptr);
551 nlists++;
554 return 0;
557 /* Called to change the V4L2 gain control value. This function
558 * rounds and clamps the given value and updates the V4L2 control value.
559 * If power is on, also updates the sensor analog and digital gains.
560 * gain is in 0.1 EV (exposure value) units.
562 static int et8ek8_set_gain(struct et8ek8_sensor *sensor, s32 gain)
564 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
565 struct et8ek8_gain new;
566 int r;
568 new = et8ek8_gain_table[gain];
570 /* FIXME: optimise I2C writes! */
571 r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
572 0x124a, new.analog >> 8);
573 if (r)
574 return r;
575 r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
576 0x1249, new.analog & 0xff);
577 if (r)
578 return r;
580 r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
581 0x124d, new.digital >> 8);
582 if (r)
583 return r;
584 r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
585 0x124c, new.digital & 0xff);
587 return r;
590 static int et8ek8_set_test_pattern(struct et8ek8_sensor *sensor, s32 mode)
592 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
593 int cbh_mode, cbv_mode, tp_mode, din_sw, r1420, rval;
595 /* Values for normal mode */
596 cbh_mode = 0;
597 cbv_mode = 0;
598 tp_mode = 0;
599 din_sw = 0x00;
600 r1420 = 0xF0;
602 if (mode) {
603 /* Test pattern mode */
604 if (mode < 5) {
605 cbh_mode = 1;
606 cbv_mode = 1;
607 tp_mode = mode + 3;
608 } else {
609 cbh_mode = 0;
610 cbv_mode = 0;
611 tp_mode = mode - 4 + 3;
614 din_sw = 0x01;
615 r1420 = 0xE0;
618 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x111B,
619 tp_mode << 4);
620 if (rval)
621 return rval;
623 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1121,
624 cbh_mode << 7);
625 if (rval)
626 return rval;
628 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1124,
629 cbv_mode << 7);
630 if (rval)
631 return rval;
633 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x112C, din_sw);
634 if (rval)
635 return rval;
637 return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1420, r1420);
640 /* -----------------------------------------------------------------------------
641 * V4L2 controls
644 static int et8ek8_set_ctrl(struct v4l2_ctrl *ctrl)
646 struct et8ek8_sensor *sensor =
647 container_of(ctrl->handler, struct et8ek8_sensor, ctrl_handler);
649 switch (ctrl->id) {
650 case V4L2_CID_GAIN:
651 return et8ek8_set_gain(sensor, ctrl->val);
653 case V4L2_CID_EXPOSURE:
655 struct i2c_client *client =
656 v4l2_get_subdevdata(&sensor->subdev);
658 return et8ek8_i2c_write_reg(client, ET8EK8_REG_16BIT, 0x1243,
659 ctrl->val);
662 case V4L2_CID_TEST_PATTERN:
663 return et8ek8_set_test_pattern(sensor, ctrl->val);
665 case V4L2_CID_PIXEL_RATE:
666 return 0;
668 default:
669 return -EINVAL;
673 static const struct v4l2_ctrl_ops et8ek8_ctrl_ops = {
674 .s_ctrl = et8ek8_set_ctrl,
677 static const char * const et8ek8_test_pattern_menu[] = {
678 "Normal",
679 "Vertical colorbar",
680 "Horizontal colorbar",
681 "Scale",
682 "Ramp",
683 "Small vertical colorbar",
684 "Small horizontal colorbar",
685 "Small scale",
686 "Small ramp",
689 static int et8ek8_init_controls(struct et8ek8_sensor *sensor)
691 s32 max_rows;
693 v4l2_ctrl_handler_init(&sensor->ctrl_handler, 4);
695 /* V4L2_CID_GAIN */
696 v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops,
697 V4L2_CID_GAIN, 0, ARRAY_SIZE(et8ek8_gain_table) - 1,
698 1, 0);
700 max_rows = sensor->current_reglist->mode.max_exp;
702 u32 min = 1, max = max_rows;
704 sensor->exposure =
705 v4l2_ctrl_new_std(&sensor->ctrl_handler,
706 &et8ek8_ctrl_ops, V4L2_CID_EXPOSURE,
707 min, max, min, max);
710 /* V4L2_CID_PIXEL_RATE */
711 sensor->pixel_rate =
712 v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops,
713 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
715 /* V4L2_CID_TEST_PATTERN */
716 v4l2_ctrl_new_std_menu_items(&sensor->ctrl_handler,
717 &et8ek8_ctrl_ops, V4L2_CID_TEST_PATTERN,
718 ARRAY_SIZE(et8ek8_test_pattern_menu) - 1,
719 0, 0, et8ek8_test_pattern_menu);
721 if (sensor->ctrl_handler.error)
722 return sensor->ctrl_handler.error;
724 sensor->subdev.ctrl_handler = &sensor->ctrl_handler;
726 return 0;
729 static void et8ek8_update_controls(struct et8ek8_sensor *sensor)
731 struct v4l2_ctrl *ctrl;
732 struct et8ek8_mode *mode = &sensor->current_reglist->mode;
734 u32 min, max, pixel_rate;
735 static const int S = 8;
737 ctrl = sensor->exposure;
739 min = 1;
740 max = mode->max_exp;
743 * Calculate average pixel clock per line. Assume buffers can spread
744 * the data over horizontal blanking time. Rounding upwards.
745 * Formula taken from stock Nokia N900 kernel.
747 pixel_rate = ((mode->pixel_clock + (1 << S) - 1) >> S) + mode->width;
748 pixel_rate = mode->window_width * (pixel_rate - 1) / mode->width;
750 __v4l2_ctrl_modify_range(ctrl, min, max, min, max);
751 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate, pixel_rate << S);
754 static int et8ek8_configure(struct et8ek8_sensor *sensor)
756 struct v4l2_subdev *subdev = &sensor->subdev;
757 struct i2c_client *client = v4l2_get_subdevdata(subdev);
758 int rval;
760 rval = et8ek8_i2c_write_regs(client, sensor->current_reglist->regs);
761 if (rval)
762 goto fail;
764 /* Controls set while the power to the sensor is turned off are saved
765 * but not applied to the hardware. Now that we're about to start
766 * streaming apply all the current values to the hardware.
768 rval = v4l2_ctrl_handler_setup(&sensor->ctrl_handler);
769 if (rval)
770 goto fail;
772 return 0;
774 fail:
775 dev_err(&client->dev, "sensor configuration failed\n");
777 return rval;
780 static int et8ek8_stream_on(struct et8ek8_sensor *sensor)
782 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
784 return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0xb0);
787 static int et8ek8_stream_off(struct et8ek8_sensor *sensor)
789 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
791 return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0x30);
794 static int et8ek8_s_stream(struct v4l2_subdev *subdev, int streaming)
796 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
797 int ret;
799 if (!streaming)
800 return et8ek8_stream_off(sensor);
802 ret = et8ek8_configure(sensor);
803 if (ret < 0)
804 return ret;
806 return et8ek8_stream_on(sensor);
809 /* --------------------------------------------------------------------------
810 * V4L2 subdev operations
813 static int et8ek8_power_off(struct et8ek8_sensor *sensor)
815 gpiod_set_value(sensor->reset, 0);
816 udelay(1);
818 clk_disable_unprepare(sensor->ext_clk);
820 return regulator_disable(sensor->vana);
823 static int et8ek8_power_on(struct et8ek8_sensor *sensor)
825 struct v4l2_subdev *subdev = &sensor->subdev;
826 struct i2c_client *client = v4l2_get_subdevdata(subdev);
827 unsigned int xclk_freq;
828 int val, rval;
830 rval = regulator_enable(sensor->vana);
831 if (rval) {
832 dev_err(&client->dev, "failed to enable vana regulator\n");
833 return rval;
836 if (sensor->current_reglist)
837 xclk_freq = sensor->current_reglist->mode.ext_clock;
838 else
839 xclk_freq = sensor->xclk_freq;
841 rval = clk_set_rate(sensor->ext_clk, xclk_freq);
842 if (rval < 0) {
843 dev_err(&client->dev, "unable to set extclk clock freq to %u\n",
844 xclk_freq);
845 goto out;
847 rval = clk_prepare_enable(sensor->ext_clk);
848 if (rval < 0) {
849 dev_err(&client->dev, "failed to enable extclk\n");
850 goto out;
853 if (rval)
854 goto out;
856 udelay(10); /* I wish this is a good value */
858 gpiod_set_value(sensor->reset, 1);
860 msleep(5000 * 1000 / xclk_freq + 1); /* Wait 5000 cycles */
862 rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist,
863 ET8EK8_REGLIST_POWERON);
864 if (rval)
865 goto out;
867 #ifdef USE_CRC
868 rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT, 0x1263, &val);
869 if (rval)
870 goto out;
871 #if USE_CRC /* TODO get crc setting from DT */
872 val |= BIT(4);
873 #else
874 val &= ~BIT(4);
875 #endif
876 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1263, val);
877 if (rval)
878 goto out;
879 #endif
881 out:
882 if (rval)
883 et8ek8_power_off(sensor);
885 return rval;
888 /* --------------------------------------------------------------------------
889 * V4L2 subdev video operations
891 #define MAX_FMTS 4
892 static int et8ek8_enum_mbus_code(struct v4l2_subdev *subdev,
893 struct v4l2_subdev_pad_config *cfg,
894 struct v4l2_subdev_mbus_code_enum *code)
896 struct et8ek8_reglist **list =
897 et8ek8_reglist_first(&meta_reglist);
898 u32 pixelformat[MAX_FMTS];
899 int npixelformat = 0;
901 if (code->index >= MAX_FMTS)
902 return -EINVAL;
904 for (; *list; list++) {
905 struct et8ek8_mode *mode = &(*list)->mode;
906 int i;
908 if ((*list)->type != ET8EK8_REGLIST_MODE)
909 continue;
911 for (i = 0; i < npixelformat; i++) {
912 if (pixelformat[i] == mode->bus_format)
913 break;
915 if (i != npixelformat)
916 continue;
918 if (code->index == npixelformat) {
919 code->code = mode->bus_format;
920 return 0;
923 pixelformat[npixelformat] = mode->bus_format;
924 npixelformat++;
927 return -EINVAL;
930 static int et8ek8_enum_frame_size(struct v4l2_subdev *subdev,
931 struct v4l2_subdev_pad_config *cfg,
932 struct v4l2_subdev_frame_size_enum *fse)
934 struct et8ek8_reglist **list =
935 et8ek8_reglist_first(&meta_reglist);
936 struct v4l2_mbus_framefmt format;
937 int cmp_width = INT_MAX;
938 int cmp_height = INT_MAX;
939 int index = fse->index;
941 for (; *list; list++) {
942 if ((*list)->type != ET8EK8_REGLIST_MODE)
943 continue;
945 et8ek8_reglist_to_mbus(*list, &format);
946 if (fse->code != format.code)
947 continue;
949 /* Assume that the modes are grouped by frame size. */
950 if (format.width == cmp_width && format.height == cmp_height)
951 continue;
953 cmp_width = format.width;
954 cmp_height = format.height;
956 if (index-- == 0) {
957 fse->min_width = format.width;
958 fse->min_height = format.height;
959 fse->max_width = format.width;
960 fse->max_height = format.height;
961 return 0;
965 return -EINVAL;
968 static int et8ek8_enum_frame_ival(struct v4l2_subdev *subdev,
969 struct v4l2_subdev_pad_config *cfg,
970 struct v4l2_subdev_frame_interval_enum *fie)
972 struct et8ek8_reglist **list =
973 et8ek8_reglist_first(&meta_reglist);
974 struct v4l2_mbus_framefmt format;
975 int index = fie->index;
977 for (; *list; list++) {
978 struct et8ek8_mode *mode = &(*list)->mode;
980 if ((*list)->type != ET8EK8_REGLIST_MODE)
981 continue;
983 et8ek8_reglist_to_mbus(*list, &format);
984 if (fie->code != format.code)
985 continue;
987 if (fie->width != format.width || fie->height != format.height)
988 continue;
990 if (index-- == 0) {
991 fie->interval = mode->timeperframe;
992 return 0;
996 return -EINVAL;
999 static struct v4l2_mbus_framefmt *
1000 __et8ek8_get_pad_format(struct et8ek8_sensor *sensor,
1001 struct v4l2_subdev_pad_config *cfg,
1002 unsigned int pad, enum v4l2_subdev_format_whence which)
1004 switch (which) {
1005 case V4L2_SUBDEV_FORMAT_TRY:
1006 return v4l2_subdev_get_try_format(&sensor->subdev, cfg, pad);
1007 case V4L2_SUBDEV_FORMAT_ACTIVE:
1008 return &sensor->format;
1009 default:
1010 return NULL;
1014 static int et8ek8_get_pad_format(struct v4l2_subdev *subdev,
1015 struct v4l2_subdev_pad_config *cfg,
1016 struct v4l2_subdev_format *fmt)
1018 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1019 struct v4l2_mbus_framefmt *format;
1021 format = __et8ek8_get_pad_format(sensor, cfg, fmt->pad, fmt->which);
1022 if (!format)
1023 return -EINVAL;
1025 fmt->format = *format;
1027 return 0;
1030 static int et8ek8_set_pad_format(struct v4l2_subdev *subdev,
1031 struct v4l2_subdev_pad_config *cfg,
1032 struct v4l2_subdev_format *fmt)
1034 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1035 struct v4l2_mbus_framefmt *format;
1036 struct et8ek8_reglist *reglist;
1038 format = __et8ek8_get_pad_format(sensor, cfg, fmt->pad, fmt->which);
1039 if (!format)
1040 return -EINVAL;
1042 reglist = et8ek8_reglist_find_mode_fmt(&meta_reglist, &fmt->format);
1043 et8ek8_reglist_to_mbus(reglist, &fmt->format);
1044 *format = fmt->format;
1046 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1047 sensor->current_reglist = reglist;
1048 et8ek8_update_controls(sensor);
1051 return 0;
1054 static int et8ek8_get_frame_interval(struct v4l2_subdev *subdev,
1055 struct v4l2_subdev_frame_interval *fi)
1057 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1059 memset(fi, 0, sizeof(*fi));
1060 fi->interval = sensor->current_reglist->mode.timeperframe;
1062 return 0;
1065 static int et8ek8_set_frame_interval(struct v4l2_subdev *subdev,
1066 struct v4l2_subdev_frame_interval *fi)
1068 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1069 struct et8ek8_reglist *reglist;
1071 reglist = et8ek8_reglist_find_mode_ival(&meta_reglist,
1072 sensor->current_reglist,
1073 &fi->interval);
1075 if (!reglist)
1076 return -EINVAL;
1078 if (sensor->current_reglist->mode.ext_clock != reglist->mode.ext_clock)
1079 return -EINVAL;
1081 sensor->current_reglist = reglist;
1082 et8ek8_update_controls(sensor);
1084 return 0;
1087 static int et8ek8_g_priv_mem(struct v4l2_subdev *subdev)
1089 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1090 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1091 unsigned int length = ET8EK8_PRIV_MEM_SIZE;
1092 unsigned int offset = 0;
1093 u8 *ptr = sensor->priv_mem;
1094 int rval = 0;
1096 /* Read the EEPROM window-by-window, each window 8 bytes */
1097 do {
1098 u8 buffer[PRIV_MEM_WIN_SIZE];
1099 struct i2c_msg msg;
1100 int bytes, i;
1101 int ofs;
1103 /* Set the current window */
1104 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x0001,
1105 0xe0 | (offset >> 3));
1106 if (rval < 0)
1107 return rval;
1109 /* Wait for status bit */
1110 for (i = 0; i < 1000; ++i) {
1111 u32 status;
1113 rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1114 0x0003, &status);
1115 if (rval < 0)
1116 return rval;
1117 if (!(status & 0x08))
1118 break;
1119 usleep_range(1000, 2000);
1122 if (i == 1000)
1123 return -EIO;
1125 /* Read window, 8 bytes at once, and copy to user space */
1126 ofs = offset & 0x07; /* Offset within this window */
1127 bytes = length + ofs > 8 ? 8-ofs : length;
1128 msg.addr = client->addr;
1129 msg.flags = 0;
1130 msg.len = 2;
1131 msg.buf = buffer;
1132 ofs += PRIV_MEM_START_REG;
1133 buffer[0] = (u8)(ofs >> 8);
1134 buffer[1] = (u8)(ofs & 0xFF);
1136 rval = i2c_transfer(client->adapter, &msg, 1);
1137 if (rval < 0)
1138 return rval;
1140 mdelay(ET8EK8_I2C_DELAY);
1141 msg.addr = client->addr;
1142 msg.len = bytes;
1143 msg.flags = I2C_M_RD;
1144 msg.buf = buffer;
1145 memset(buffer, 0, sizeof(buffer));
1147 rval = i2c_transfer(client->adapter, &msg, 1);
1148 if (rval < 0)
1149 return rval;
1151 rval = 0;
1152 memcpy(ptr, buffer, bytes);
1154 length -= bytes;
1155 offset += bytes;
1156 ptr += bytes;
1157 } while (length > 0);
1159 return rval;
1162 static int et8ek8_dev_init(struct v4l2_subdev *subdev)
1164 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1165 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1166 int rval, rev_l, rev_h;
1168 rval = et8ek8_power_on(sensor);
1169 if (rval) {
1170 dev_err(&client->dev, "could not power on\n");
1171 return rval;
1174 rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1175 REG_REVISION_NUMBER_L, &rev_l);
1176 if (!rval)
1177 rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1178 REG_REVISION_NUMBER_H, &rev_h);
1179 if (rval) {
1180 dev_err(&client->dev, "no et8ek8 sensor detected\n");
1181 goto out_poweroff;
1184 sensor->version = (rev_h << 8) + rev_l;
1185 if (sensor->version != ET8EK8_REV_1 && sensor->version != ET8EK8_REV_2)
1186 dev_info(&client->dev,
1187 "unknown version 0x%x detected, continuing anyway\n",
1188 sensor->version);
1190 rval = et8ek8_reglist_import(client, &meta_reglist);
1191 if (rval) {
1192 dev_err(&client->dev,
1193 "invalid register list %s, import failed\n",
1194 ET8EK8_NAME);
1195 goto out_poweroff;
1198 sensor->current_reglist = et8ek8_reglist_find_type(&meta_reglist,
1199 ET8EK8_REGLIST_MODE);
1200 if (!sensor->current_reglist) {
1201 dev_err(&client->dev,
1202 "invalid register list %s, no mode found\n",
1203 ET8EK8_NAME);
1204 rval = -ENODEV;
1205 goto out_poweroff;
1208 et8ek8_reglist_to_mbus(sensor->current_reglist, &sensor->format);
1210 rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist,
1211 ET8EK8_REGLIST_POWERON);
1212 if (rval) {
1213 dev_err(&client->dev,
1214 "invalid register list %s, no POWERON mode found\n",
1215 ET8EK8_NAME);
1216 goto out_poweroff;
1218 rval = et8ek8_stream_on(sensor); /* Needed to be able to read EEPROM */
1219 if (rval)
1220 goto out_poweroff;
1221 rval = et8ek8_g_priv_mem(subdev);
1222 if (rval)
1223 dev_warn(&client->dev,
1224 "can not read OTP (EEPROM) memory from sensor\n");
1225 rval = et8ek8_stream_off(sensor);
1226 if (rval)
1227 goto out_poweroff;
1229 rval = et8ek8_power_off(sensor);
1230 if (rval)
1231 goto out_poweroff;
1233 return 0;
1235 out_poweroff:
1236 et8ek8_power_off(sensor);
1238 return rval;
1241 /* --------------------------------------------------------------------------
1242 * sysfs attributes
1244 static ssize_t
1245 et8ek8_priv_mem_read(struct device *dev, struct device_attribute *attr,
1246 char *buf)
1248 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
1249 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1251 #if PAGE_SIZE < ET8EK8_PRIV_MEM_SIZE
1252 #error PAGE_SIZE too small!
1253 #endif
1255 memcpy(buf, sensor->priv_mem, ET8EK8_PRIV_MEM_SIZE);
1257 return ET8EK8_PRIV_MEM_SIZE;
1259 static DEVICE_ATTR(priv_mem, 0444, et8ek8_priv_mem_read, NULL);
1261 /* --------------------------------------------------------------------------
1262 * V4L2 subdev core operations
1265 static int
1266 et8ek8_registered(struct v4l2_subdev *subdev)
1268 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1269 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1270 int rval;
1272 dev_dbg(&client->dev, "registered!");
1274 rval = device_create_file(&client->dev, &dev_attr_priv_mem);
1275 if (rval) {
1276 dev_err(&client->dev, "could not register sysfs entry\n");
1277 return rval;
1280 rval = et8ek8_dev_init(subdev);
1281 if (rval)
1282 goto err_file;
1284 rval = et8ek8_init_controls(sensor);
1285 if (rval) {
1286 dev_err(&client->dev, "controls initialization failed\n");
1287 goto err_file;
1290 __et8ek8_get_pad_format(sensor, NULL, 0, V4L2_SUBDEV_FORMAT_ACTIVE);
1292 return 0;
1294 err_file:
1295 device_remove_file(&client->dev, &dev_attr_priv_mem);
1297 return rval;
1300 static int __et8ek8_set_power(struct et8ek8_sensor *sensor, bool on)
1302 return on ? et8ek8_power_on(sensor) : et8ek8_power_off(sensor);
1305 static int et8ek8_set_power(struct v4l2_subdev *subdev, int on)
1307 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1308 int ret = 0;
1310 mutex_lock(&sensor->power_lock);
1312 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
1313 * update the power state.
1315 if (sensor->power_count == !on) {
1316 ret = __et8ek8_set_power(sensor, !!on);
1317 if (ret < 0)
1318 goto done;
1321 /* Update the power count. */
1322 sensor->power_count += on ? 1 : -1;
1323 WARN_ON(sensor->power_count < 0);
1325 done:
1326 mutex_unlock(&sensor->power_lock);
1328 return ret;
1331 static int et8ek8_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1333 struct et8ek8_sensor *sensor = to_et8ek8_sensor(sd);
1334 struct v4l2_mbus_framefmt *format;
1335 struct et8ek8_reglist *reglist;
1337 reglist = et8ek8_reglist_find_type(&meta_reglist, ET8EK8_REGLIST_MODE);
1338 format = __et8ek8_get_pad_format(sensor, fh->pad, 0,
1339 V4L2_SUBDEV_FORMAT_TRY);
1340 et8ek8_reglist_to_mbus(reglist, format);
1342 return et8ek8_set_power(sd, true);
1345 static int et8ek8_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1347 return et8ek8_set_power(sd, false);
1350 static const struct v4l2_subdev_video_ops et8ek8_video_ops = {
1351 .s_stream = et8ek8_s_stream,
1352 .g_frame_interval = et8ek8_get_frame_interval,
1353 .s_frame_interval = et8ek8_set_frame_interval,
1356 static const struct v4l2_subdev_core_ops et8ek8_core_ops = {
1357 .s_power = et8ek8_set_power,
1360 static const struct v4l2_subdev_pad_ops et8ek8_pad_ops = {
1361 .enum_mbus_code = et8ek8_enum_mbus_code,
1362 .enum_frame_size = et8ek8_enum_frame_size,
1363 .enum_frame_interval = et8ek8_enum_frame_ival,
1364 .get_fmt = et8ek8_get_pad_format,
1365 .set_fmt = et8ek8_set_pad_format,
1368 static const struct v4l2_subdev_ops et8ek8_ops = {
1369 .core = &et8ek8_core_ops,
1370 .video = &et8ek8_video_ops,
1371 .pad = &et8ek8_pad_ops,
1374 static const struct v4l2_subdev_internal_ops et8ek8_internal_ops = {
1375 .registered = et8ek8_registered,
1376 .open = et8ek8_open,
1377 .close = et8ek8_close,
1380 /* --------------------------------------------------------------------------
1381 * I2C driver
1383 static int __maybe_unused et8ek8_suspend(struct device *dev)
1385 struct i2c_client *client = to_i2c_client(dev);
1386 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1387 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1389 if (!sensor->power_count)
1390 return 0;
1392 return __et8ek8_set_power(sensor, false);
1395 static int __maybe_unused et8ek8_resume(struct device *dev)
1397 struct i2c_client *client = to_i2c_client(dev);
1398 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1399 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1401 if (!sensor->power_count)
1402 return 0;
1404 return __et8ek8_set_power(sensor, true);
1407 static int et8ek8_probe(struct i2c_client *client,
1408 const struct i2c_device_id *devid)
1410 struct et8ek8_sensor *sensor;
1411 struct device *dev = &client->dev;
1412 int ret;
1414 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
1415 if (!sensor)
1416 return -ENOMEM;
1418 sensor->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1419 if (IS_ERR(sensor->reset)) {
1420 dev_dbg(&client->dev, "could not request reset gpio\n");
1421 return PTR_ERR(sensor->reset);
1424 sensor->vana = devm_regulator_get(dev, "vana");
1425 if (IS_ERR(sensor->vana)) {
1426 dev_err(&client->dev, "could not get regulator for vana\n");
1427 return PTR_ERR(sensor->vana);
1430 sensor->ext_clk = devm_clk_get(dev, NULL);
1431 if (IS_ERR(sensor->ext_clk)) {
1432 dev_err(&client->dev, "could not get clock\n");
1433 return PTR_ERR(sensor->ext_clk);
1436 ret = of_property_read_u32(dev->of_node, "clock-frequency",
1437 &sensor->xclk_freq);
1438 if (ret) {
1439 dev_warn(dev, "can't get clock-frequency\n");
1440 return ret;
1443 mutex_init(&sensor->power_lock);
1445 v4l2_i2c_subdev_init(&sensor->subdev, client, &et8ek8_ops);
1446 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1447 sensor->subdev.internal_ops = &et8ek8_internal_ops;
1449 sensor->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1450 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1451 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
1452 if (ret < 0) {
1453 dev_err(&client->dev, "media entity init failed!\n");
1454 goto err_mutex;
1457 ret = v4l2_async_register_subdev_sensor_common(&sensor->subdev);
1458 if (ret < 0)
1459 goto err_entity;
1461 dev_dbg(dev, "initialized!\n");
1463 return 0;
1465 err_entity:
1466 media_entity_cleanup(&sensor->subdev.entity);
1467 err_mutex:
1468 mutex_destroy(&sensor->power_lock);
1469 return ret;
1472 static int __exit et8ek8_remove(struct i2c_client *client)
1474 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1475 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1477 if (sensor->power_count) {
1478 WARN_ON(1);
1479 et8ek8_power_off(sensor);
1480 sensor->power_count = 0;
1483 v4l2_device_unregister_subdev(&sensor->subdev);
1484 device_remove_file(&client->dev, &dev_attr_priv_mem);
1485 v4l2_ctrl_handler_free(&sensor->ctrl_handler);
1486 v4l2_async_unregister_subdev(&sensor->subdev);
1487 media_entity_cleanup(&sensor->subdev.entity);
1488 mutex_destroy(&sensor->power_lock);
1490 return 0;
1493 static const struct of_device_id et8ek8_of_table[] = {
1494 { .compatible = "toshiba,et8ek8" },
1495 { },
1497 MODULE_DEVICE_TABLE(of, et8ek8_of_table);
1499 static const struct i2c_device_id et8ek8_id_table[] = {
1500 { ET8EK8_NAME, 0 },
1503 MODULE_DEVICE_TABLE(i2c, et8ek8_id_table);
1505 static const struct dev_pm_ops et8ek8_pm_ops = {
1506 SET_SYSTEM_SLEEP_PM_OPS(et8ek8_suspend, et8ek8_resume)
1509 static struct i2c_driver et8ek8_i2c_driver = {
1510 .driver = {
1511 .name = ET8EK8_NAME,
1512 .pm = &et8ek8_pm_ops,
1513 .of_match_table = et8ek8_of_table,
1515 .probe = et8ek8_probe,
1516 .remove = __exit_p(et8ek8_remove),
1517 .id_table = et8ek8_id_table,
1520 module_i2c_driver(et8ek8_i2c_driver);
1522 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>, Pavel Machek <pavel@ucw.cz");
1523 MODULE_DESCRIPTION("Toshiba ET8EK8 camera sensor driver");
1524 MODULE_LICENSE("GPL");