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
;
51 struct v4l2_mbus_framefmt format
;
52 struct gpio_desc
*reset
;
53 struct regulator
*vana
;
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
;
70 #define to_et8ek8_sensor(sd) container_of(sd, struct et8ek8_sensor, subdev)
72 enum et8ek8_versions
{
73 ET8EK8_REV_1
= 0x0001,
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
{
88 } const et8ek8_gain_table
[] = {
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 */
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
,
154 unsigned char data
[4];
156 if (!client
->adapter
)
158 if (data_length
!= ET8EK8_REG_8BIT
&& data_length
!= ET8EK8_REG_16BIT
)
161 msg
.addr
= client
->addr
;
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);
173 msg
.len
= data_length
;
174 msg
.flags
= I2C_M_RD
;
175 r
= i2c_transfer(client
->adapter
, &msg
, 1);
180 /* high byte comes first */
181 if (data_length
== ET8EK8_REG_8BIT
)
184 *val
= (data
[1] << 8) + data
[0];
189 dev_err(&client
->dev
, "read from offset 0x%x error %d\n", reg
, r
);
194 static void et8ek8_i2c_create_msg(struct i2c_client
*client
, u16 len
, u16 reg
,
195 u32 val
, struct i2c_msg
*msg
,
198 msg
->addr
= client
->addr
;
199 msg
->flags
= 0; /* Write */
203 /* high byte goes out first */
204 buf
[0] = (u8
) (reg
>> 8);
205 buf
[1] = (u8
) (reg
& 0xff);
208 case ET8EK8_REG_8BIT
:
209 buf
[2] = (u8
) (val
) & 0xff;
211 case ET8EK8_REG_16BIT
:
212 buf
[2] = (u8
) (val
) & 0xff;
213 buf
[3] = (u8
) (val
>> 8) & 0xff;
216 WARN_ONCE(1, ET8EK8_NAME
": %s: invalid message length.\n",
222 * A buffered write method that puts the wanted register write
223 * commands in smaller number of message lists and passes the lists to
226 static int et8ek8_i2c_buffered_write_regs(struct i2c_client
*client
,
227 const struct et8ek8_reg
*wnext
,
230 struct i2c_msg msg
[ET8EK8_MAX_MSG
];
231 unsigned char data
[ET8EK8_MAX_MSG
][6];
233 u16 reg
, data_length
;
237 /* Create new write messages for all writes */
239 data_length
= wnext
->type
;
244 et8ek8_i2c_create_msg(client
, data_length
, reg
,
245 val
, &msg
[wcnt
], &data
[wcnt
][0]);
247 /* Update write count */
250 if (wcnt
< ET8EK8_MAX_MSG
)
253 rval
= i2c_transfer(client
->adapter
, msg
, wcnt
);
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
)
276 const struct et8ek8_reg
*next
;
278 if (!client
->adapter
)
284 /* Initialize list pointers to the start of the list */
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
298 if (WARN(next
->type
!= ET8EK8_REG_8BIT
&&
299 next
->type
!= ET8EK8_REG_16BIT
,
300 "Invalid type = %d", next
->type
)) {
304 * Increment count of successive writes and
311 /* Now we start writing ... */
312 r
= et8ek8_i2c_buffered_write_regs(client
, regs
, cnt
);
314 /* ... and then check that everything was OK */
316 dev_err(&client
->dev
, "i2c transfer error!\n");
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
) {
328 * Update list pointers and cnt and start over ...
334 } while (next
->type
!= ET8EK8_REG_TERM
);
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
,
348 unsigned char data
[6];
350 if (!client
->adapter
)
352 if (data_length
!= ET8EK8_REG_8BIT
&& data_length
!= ET8EK8_REG_16BIT
)
355 et8ek8_i2c_create_msg(client
, data_length
, reg
, val
, &msg
, data
);
357 r
= i2c_transfer(client
->adapter
, &msg
, 1);
359 dev_err(&client
->dev
,
360 "wrote 0x%x to offset 0x%x error %d\n", val
, reg
, r
);
367 static struct et8ek8_reglist
*et8ek8_reglist_find_type(
368 struct et8ek8_meta_reglist
*meta
,
371 struct et8ek8_reglist
**next
= &meta
->reglist
[0].ptr
;
374 if ((*next
)->type
== type
)
383 static int et8ek8_i2c_reglist_find_write(struct i2c_client
*client
,
384 struct et8ek8_meta_reglist
*meta
,
387 struct et8ek8_reglist
*reglist
;
389 reglist
= et8ek8_reglist_find_type(meta
, type
);
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
431 for (; *list
; list
++) {
434 if ((*list
)->type
!= ET8EK8_REGLIST_MODE
)
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
) {
448 max_dist_match
= dist
;
451 if (dist
< max_dist_other
|| !best_other
) {
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
= ¤t_reglist
->mode
;
473 for (; *list
; list
++) {
474 struct et8ek8_mode
*mode
= &(*list
)->mode
;
476 if ((*list
)->type
!= ET8EK8_REGLIST_MODE
)
479 if (mode
->window_width
!= current_mode
->window_width
||
480 mode
->window_height
!= current_mode
->window_height
)
483 if (TIMEPERFRAME_AVG_FPS(mode
->timeperframe
) == fps
)
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
)
499 if ((*list1
)->type
!= ET8EK8_REGLIST_MODE
&&
500 (*list2
)->type
== ET8EK8_REGLIST_MODE
)
503 /* Descending width. */
504 if ((*list1
)->mode
.window_width
> (*list2
)->mode
.window_width
)
506 if ((*list1
)->mode
.window_width
< (*list2
)->mode
.window_width
)
509 if ((*list1
)->mode
.window_height
> (*list2
)->mode
.window_height
)
511 if ((*list1
)->mode
.window_height
< (*list2
)->mode
.window_height
)
517 static int et8ek8_reglist_import(struct i2c_client
*client
,
518 struct et8ek8_meta_reglist
*meta
)
522 dev_info(&client
->dev
, "meta_reglist version %s\n", meta
->version
);
524 while (meta
->reglist
[nlists
].ptr
)
530 sort(&meta
->reglist
[0].ptr
, nlists
, sizeof(meta
->reglist
[0].ptr
),
531 et8ek8_reglist_cmp
, NULL
);
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",
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
);
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;
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);
575 r
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
,
576 0x1249, new.analog
& 0xff);
580 r
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
,
581 0x124d, new.digital
>> 8);
584 r
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
,
585 0x124c, new.digital
& 0xff);
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 */
603 /* Test pattern mode */
611 tp_mode
= mode
- 4 + 3;
618 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x111B,
623 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1121,
628 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1124,
633 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x112C, din_sw
);
637 return et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1420, r1420
);
640 /* -----------------------------------------------------------------------------
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
);
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,
662 case V4L2_CID_TEST_PATTERN
:
663 return et8ek8_set_test_pattern(sensor
, ctrl
->val
);
665 case V4L2_CID_PIXEL_RATE
:
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
[] = {
680 "Horizontal colorbar",
683 "Small vertical colorbar",
684 "Small horizontal colorbar",
689 static int et8ek8_init_controls(struct et8ek8_sensor
*sensor
)
693 v4l2_ctrl_handler_init(&sensor
->ctrl_handler
, 4);
696 v4l2_ctrl_new_std(&sensor
->ctrl_handler
, &et8ek8_ctrl_ops
,
697 V4L2_CID_GAIN
, 0, ARRAY_SIZE(et8ek8_gain_table
) - 1,
700 max_rows
= sensor
->current_reglist
->mode
.max_exp
;
702 u32 min
= 1, max
= max_rows
;
705 v4l2_ctrl_new_std(&sensor
->ctrl_handler
,
706 &et8ek8_ctrl_ops
, V4L2_CID_EXPOSURE
,
710 /* V4L2_CID_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
;
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
;
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
);
760 rval
= et8ek8_i2c_write_regs(client
, sensor
->current_reglist
->regs
);
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
);
775 dev_err(&client
->dev
, "sensor configuration failed\n");
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
);
800 return et8ek8_stream_off(sensor
);
802 ret
= et8ek8_configure(sensor
);
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);
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
;
830 rval
= regulator_enable(sensor
->vana
);
832 dev_err(&client
->dev
, "failed to enable vana regulator\n");
836 if (sensor
->current_reglist
)
837 xclk_freq
= sensor
->current_reglist
->mode
.ext_clock
;
839 xclk_freq
= sensor
->xclk_freq
;
841 rval
= clk_set_rate(sensor
->ext_clk
, xclk_freq
);
843 dev_err(&client
->dev
, "unable to set extclk clock freq to %u\n",
847 rval
= clk_prepare_enable(sensor
->ext_clk
);
849 dev_err(&client
->dev
, "failed to enable extclk\n");
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
);
868 rval
= et8ek8_i2c_read_reg(client
, ET8EK8_REG_8BIT
, 0x1263, &val
);
871 #if USE_CRC /* TODO get crc setting from DT */
876 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1263, val
);
883 et8ek8_power_off(sensor
);
888 /* --------------------------------------------------------------------------
889 * V4L2 subdev video operations
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
)
904 for (; *list
; list
++) {
905 struct et8ek8_mode
*mode
= &(*list
)->mode
;
908 if ((*list
)->type
!= ET8EK8_REGLIST_MODE
)
911 for (i
= 0; i
< npixelformat
; i
++) {
912 if (pixelformat
[i
] == mode
->bus_format
)
915 if (i
!= npixelformat
)
918 if (code
->index
== npixelformat
) {
919 code
->code
= mode
->bus_format
;
923 pixelformat
[npixelformat
] = mode
->bus_format
;
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
)
945 et8ek8_reglist_to_mbus(*list
, &format
);
946 if (fse
->code
!= format
.code
)
949 /* Assume that the modes are grouped by frame size. */
950 if (format
.width
== cmp_width
&& format
.height
== cmp_height
)
953 cmp_width
= format
.width
;
954 cmp_height
= format
.height
;
957 fse
->min_width
= format
.width
;
958 fse
->min_height
= format
.height
;
959 fse
->max_width
= format
.width
;
960 fse
->max_height
= format
.height
;
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
)
983 et8ek8_reglist_to_mbus(*list
, &format
);
984 if (fie
->code
!= format
.code
)
987 if (fie
->width
!= format
.width
|| fie
->height
!= format
.height
)
991 fie
->interval
= mode
->timeperframe
;
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
)
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
;
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
);
1025 fmt
->format
= *format
;
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
);
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
);
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
;
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
,
1078 if (sensor
->current_reglist
->mode
.ext_clock
!= reglist
->mode
.ext_clock
)
1081 sensor
->current_reglist
= reglist
;
1082 et8ek8_update_controls(sensor
);
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
;
1096 /* Read the EEPROM window-by-window, each window 8 bytes */
1098 u8 buffer
[PRIV_MEM_WIN_SIZE
];
1103 /* Set the current window */
1104 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x0001,
1105 0xe0 | (offset
>> 3));
1109 /* Wait for status bit */
1110 for (i
= 0; i
< 1000; ++i
) {
1113 rval
= et8ek8_i2c_read_reg(client
, ET8EK8_REG_8BIT
,
1117 if (!(status
& 0x08))
1119 usleep_range(1000, 2000);
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
;
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);
1140 mdelay(ET8EK8_I2C_DELAY
);
1141 msg
.addr
= client
->addr
;
1143 msg
.flags
= I2C_M_RD
;
1145 memset(buffer
, 0, sizeof(buffer
));
1147 rval
= i2c_transfer(client
->adapter
, &msg
, 1);
1152 memcpy(ptr
, buffer
, bytes
);
1157 } while (length
> 0);
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
);
1170 dev_err(&client
->dev
, "could not power on\n");
1174 rval
= et8ek8_i2c_read_reg(client
, ET8EK8_REG_8BIT
,
1175 REG_REVISION_NUMBER_L
, &rev_l
);
1177 rval
= et8ek8_i2c_read_reg(client
, ET8EK8_REG_8BIT
,
1178 REG_REVISION_NUMBER_H
, &rev_h
);
1180 dev_err(&client
->dev
, "no et8ek8 sensor detected\n");
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",
1190 rval
= et8ek8_reglist_import(client
, &meta_reglist
);
1192 dev_err(&client
->dev
,
1193 "invalid register list %s, import failed\n",
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",
1208 et8ek8_reglist_to_mbus(sensor
->current_reglist
, &sensor
->format
);
1210 rval
= et8ek8_i2c_reglist_find_write(client
, &meta_reglist
,
1211 ET8EK8_REGLIST_POWERON
);
1213 dev_err(&client
->dev
,
1214 "invalid register list %s, no POWERON mode found\n",
1218 rval
= et8ek8_stream_on(sensor
); /* Needed to be able to read EEPROM */
1221 rval
= et8ek8_g_priv_mem(subdev
);
1223 dev_warn(&client
->dev
,
1224 "can not read OTP (EEPROM) memory from sensor\n");
1225 rval
= et8ek8_stream_off(sensor
);
1229 rval
= et8ek8_power_off(sensor
);
1236 et8ek8_power_off(sensor
);
1241 /* --------------------------------------------------------------------------
1245 et8ek8_priv_mem_read(struct device
*dev
, struct device_attribute
*attr
,
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!
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
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
);
1272 dev_dbg(&client
->dev
, "registered!");
1274 rval
= device_create_file(&client
->dev
, &dev_attr_priv_mem
);
1276 dev_err(&client
->dev
, "could not register sysfs entry\n");
1280 rval
= et8ek8_dev_init(subdev
);
1284 rval
= et8ek8_init_controls(sensor
);
1286 dev_err(&client
->dev
, "controls initialization failed\n");
1290 __et8ek8_get_pad_format(sensor
, NULL
, 0, V4L2_SUBDEV_FORMAT_ACTIVE
);
1295 device_remove_file(&client
->dev
, &dev_attr_priv_mem
);
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
);
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
);
1321 /* Update the power count. */
1322 sensor
->power_count
+= on
? 1 : -1;
1323 WARN_ON(sensor
->power_count
< 0);
1326 mutex_unlock(&sensor
->power_lock
);
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 /* --------------------------------------------------------------------------
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
)
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
)
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
;
1414 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
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
);
1439 dev_warn(dev
, "can't get clock-frequency\n");
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
);
1453 dev_err(&client
->dev
, "media entity init failed!\n");
1457 ret
= v4l2_async_register_subdev_sensor_common(&sensor
->subdev
);
1461 dev_dbg(dev
, "initialized!\n");
1466 media_entity_cleanup(&sensor
->subdev
.entity
);
1468 mutex_destroy(&sensor
->power_lock
);
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
) {
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
);
1493 static const struct of_device_id et8ek8_of_table
[] = {
1494 { .compatible
= "toshiba,et8ek8" },
1497 MODULE_DEVICE_TABLE(of
, et8ek8_of_table
);
1499 static const struct i2c_device_id et8ek8_id_table
[] = {
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
= {
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");