1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2008 Nokia Corporation
7 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
8 * Tuukka Toivonen <tuukkat76@gmail.com>
9 * Pavel Machek <pavel@ucw.cz>
11 * Based on code from Toni Leinonen <toni.leinonen@offcode.fi>.
13 * This driver is based on the Micron MT9T012 camera imager driver
14 * (C) Texas Instruments.
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <linux/sort.h>
27 #include <linux/v4l2-mediabus.h>
29 #include <media/media-entity.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-subdev.h>
34 #include "et8ek8_reg.h"
36 #define ET8EK8_NAME "et8ek8"
37 #define ET8EK8_PRIV_MEM_SIZE 128
38 #define ET8EK8_MAX_MSG 8
40 struct et8ek8_sensor
{
41 struct v4l2_subdev subdev
;
43 struct v4l2_mbus_framefmt format
;
44 struct gpio_desc
*reset
;
45 struct regulator
*vana
;
51 struct v4l2_ctrl_handler ctrl_handler
;
52 struct v4l2_ctrl
*exposure
;
53 struct v4l2_ctrl
*pixel_rate
;
54 struct et8ek8_reglist
*current_reglist
;
56 u8 priv_mem
[ET8EK8_PRIV_MEM_SIZE
];
58 struct mutex power_lock
;
62 #define to_et8ek8_sensor(sd) container_of(sd, struct et8ek8_sensor, subdev)
64 enum et8ek8_versions
{
65 ET8EK8_REV_1
= 0x0001,
70 * This table describes what should be written to the sensor register
71 * for each gain value. The gain(index in the table) is in terms of
72 * 0.1EV, i.e. 10 indexes in the table give 2 time more gain [0] in
73 * the *analog gain, [1] in the digital gain
75 * Analog gain [dB] = 20*log10(regvalue/32); 0x20..0x100
77 static struct et8ek8_gain
{
80 } const et8ek8_gain_table
[] = {
121 {256, 1023}, /* x16 */
124 /* Register definitions */
125 #define REG_REVISION_NUMBER_L 0x1200
126 #define REG_REVISION_NUMBER_H 0x1201
128 #define PRIV_MEM_START_REG 0x0008
129 #define PRIV_MEM_WIN_SIZE 8
131 #define ET8EK8_I2C_DELAY 3 /* msec delay b/w accesses */
136 * Register access helpers
138 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
139 * Returns zero if successful, or non-zero otherwise.
141 static int et8ek8_i2c_read_reg(struct i2c_client
*client
, u16 data_length
,
146 unsigned char data
[4];
148 if (!client
->adapter
)
150 if (data_length
!= ET8EK8_REG_8BIT
&& data_length
!= ET8EK8_REG_16BIT
)
153 msg
.addr
= client
->addr
;
158 /* high byte goes out first */
159 data
[0] = (u8
) (reg
>> 8);
160 data
[1] = (u8
) (reg
& 0xff);
161 r
= i2c_transfer(client
->adapter
, &msg
, 1);
165 msg
.len
= data_length
;
166 msg
.flags
= I2C_M_RD
;
167 r
= i2c_transfer(client
->adapter
, &msg
, 1);
172 /* high byte comes first */
173 if (data_length
== ET8EK8_REG_8BIT
)
176 *val
= (data
[1] << 8) + data
[0];
181 dev_err(&client
->dev
, "read from offset 0x%x error %d\n", reg
, r
);
186 static void et8ek8_i2c_create_msg(struct i2c_client
*client
, u16 len
, u16 reg
,
187 u32 val
, struct i2c_msg
*msg
,
190 msg
->addr
= client
->addr
;
191 msg
->flags
= 0; /* Write */
195 /* high byte goes out first */
196 buf
[0] = (u8
) (reg
>> 8);
197 buf
[1] = (u8
) (reg
& 0xff);
200 case ET8EK8_REG_8BIT
:
201 buf
[2] = (u8
) (val
) & 0xff;
203 case ET8EK8_REG_16BIT
:
204 buf
[2] = (u8
) (val
) & 0xff;
205 buf
[3] = (u8
) (val
>> 8) & 0xff;
208 WARN_ONCE(1, ET8EK8_NAME
": %s: invalid message length.\n",
214 * A buffered write method that puts the wanted register write
215 * commands in smaller number of message lists and passes the lists to
218 static int et8ek8_i2c_buffered_write_regs(struct i2c_client
*client
,
219 const struct et8ek8_reg
*wnext
,
222 struct i2c_msg msg
[ET8EK8_MAX_MSG
];
223 unsigned char data
[ET8EK8_MAX_MSG
][6];
225 u16 reg
, data_length
;
229 /* Create new write messages for all writes */
231 data_length
= wnext
->type
;
236 et8ek8_i2c_create_msg(client
, data_length
, reg
,
237 val
, &msg
[wcnt
], &data
[wcnt
][0]);
239 /* Update write count */
242 if (wcnt
< ET8EK8_MAX_MSG
)
245 rval
= i2c_transfer(client
->adapter
, msg
, wcnt
);
253 rval
= i2c_transfer(client
->adapter
, msg
, wcnt
);
255 return rval
< 0 ? rval
: 0;
259 * Write a list of registers to i2c device.
261 * The list of registers is terminated by ET8EK8_REG_TERM.
262 * Returns zero if successful, or non-zero otherwise.
264 static int et8ek8_i2c_write_regs(struct i2c_client
*client
,
265 const struct et8ek8_reg
*regs
)
268 const struct et8ek8_reg
*next
;
270 if (!client
->adapter
)
276 /* Initialize list pointers to the start of the list */
281 * We have to go through the list to figure out how
282 * many regular writes we have in a row
284 while (next
->type
!= ET8EK8_REG_TERM
&&
285 next
->type
!= ET8EK8_REG_DELAY
) {
287 * Here we check that the actual length fields
290 if (WARN(next
->type
!= ET8EK8_REG_8BIT
&&
291 next
->type
!= ET8EK8_REG_16BIT
,
292 "Invalid type = %d", next
->type
)) {
296 * Increment count of successive writes and
303 /* Now we start writing ... */
304 r
= et8ek8_i2c_buffered_write_regs(client
, regs
, cnt
);
306 /* ... and then check that everything was OK */
308 dev_err(&client
->dev
, "i2c transfer error!\n");
313 * If we ran into a sleep statement when going through
314 * the list, this is where we snooze for the required time
316 if (next
->type
== ET8EK8_REG_DELAY
) {
320 * Update list pointers and cnt and start over ...
326 } while (next
->type
!= ET8EK8_REG_TERM
);
332 * Write to a 8/16-bit register.
333 * Returns zero if successful, or non-zero otherwise.
335 static int et8ek8_i2c_write_reg(struct i2c_client
*client
, u16 data_length
,
340 unsigned char data
[6];
342 if (!client
->adapter
)
344 if (data_length
!= ET8EK8_REG_8BIT
&& data_length
!= ET8EK8_REG_16BIT
)
347 et8ek8_i2c_create_msg(client
, data_length
, reg
, val
, &msg
, data
);
349 r
= i2c_transfer(client
->adapter
, &msg
, 1);
351 dev_err(&client
->dev
,
352 "wrote 0x%x to offset 0x%x error %d\n", val
, reg
, r
);
359 static struct et8ek8_reglist
*et8ek8_reglist_find_type(
360 struct et8ek8_meta_reglist
*meta
,
363 struct et8ek8_reglist
**next
= &meta
->reglist
[0].ptr
;
366 if ((*next
)->type
== type
)
375 static int et8ek8_i2c_reglist_find_write(struct i2c_client
*client
,
376 struct et8ek8_meta_reglist
*meta
,
379 struct et8ek8_reglist
*reglist
;
381 reglist
= et8ek8_reglist_find_type(meta
, type
);
385 return et8ek8_i2c_write_regs(client
, reglist
->regs
);
388 static struct et8ek8_reglist
**et8ek8_reglist_first(
389 struct et8ek8_meta_reglist
*meta
)
391 return &meta
->reglist
[0].ptr
;
394 static void et8ek8_reglist_to_mbus(const struct et8ek8_reglist
*reglist
,
395 struct v4l2_mbus_framefmt
*fmt
)
397 fmt
->width
= reglist
->mode
.window_width
;
398 fmt
->height
= reglist
->mode
.window_height
;
399 fmt
->code
= reglist
->mode
.bus_format
;
402 static struct et8ek8_reglist
*et8ek8_reglist_find_mode_fmt(
403 struct et8ek8_meta_reglist
*meta
,
404 struct v4l2_mbus_framefmt
*fmt
)
406 struct et8ek8_reglist
**list
= et8ek8_reglist_first(meta
);
407 struct et8ek8_reglist
*best_match
= NULL
;
408 struct et8ek8_reglist
*best_other
= NULL
;
409 struct v4l2_mbus_framefmt format
;
410 unsigned int max_dist_match
= (unsigned int)-1;
411 unsigned int max_dist_other
= (unsigned int)-1;
414 * Find the mode with the closest image size. The distance between
415 * image sizes is the size in pixels of the non-overlapping regions
416 * between the requested size and the frame-specified size.
418 * Store both the closest mode that matches the requested format, and
419 * the closest mode for all other formats. The best match is returned
420 * if found, otherwise the best mode with a non-matching format is
423 for (; *list
; list
++) {
426 if ((*list
)->type
!= ET8EK8_REGLIST_MODE
)
429 et8ek8_reglist_to_mbus(*list
, &format
);
431 dist
= min(fmt
->width
, format
.width
)
432 * min(fmt
->height
, format
.height
);
433 dist
= format
.width
* format
.height
434 + fmt
->width
* fmt
->height
- 2 * dist
;
437 if (fmt
->code
== format
.code
) {
438 if (dist
< max_dist_match
|| !best_match
) {
440 max_dist_match
= dist
;
443 if (dist
< max_dist_other
|| !best_other
) {
445 max_dist_other
= dist
;
450 return best_match
? best_match
: best_other
;
453 #define TIMEPERFRAME_AVG_FPS(t) \
454 (((t).denominator + ((t).numerator >> 1)) / (t).numerator)
456 static struct et8ek8_reglist
*et8ek8_reglist_find_mode_ival(
457 struct et8ek8_meta_reglist
*meta
,
458 struct et8ek8_reglist
*current_reglist
,
459 struct v4l2_fract
*timeperframe
)
461 int fps
= TIMEPERFRAME_AVG_FPS(*timeperframe
);
462 struct et8ek8_reglist
**list
= et8ek8_reglist_first(meta
);
463 struct et8ek8_mode
*current_mode
= ¤t_reglist
->mode
;
465 for (; *list
; list
++) {
466 struct et8ek8_mode
*mode
= &(*list
)->mode
;
468 if ((*list
)->type
!= ET8EK8_REGLIST_MODE
)
471 if (mode
->window_width
!= current_mode
->window_width
||
472 mode
->window_height
!= current_mode
->window_height
)
475 if (TIMEPERFRAME_AVG_FPS(mode
->timeperframe
) == fps
)
482 static int et8ek8_reglist_cmp(const void *a
, const void *b
)
484 const struct et8ek8_reglist
**list1
= (const struct et8ek8_reglist
**)a
,
485 **list2
= (const struct et8ek8_reglist
**)b
;
487 /* Put real modes in the beginning. */
488 if ((*list1
)->type
== ET8EK8_REGLIST_MODE
&&
489 (*list2
)->type
!= ET8EK8_REGLIST_MODE
)
491 if ((*list1
)->type
!= ET8EK8_REGLIST_MODE
&&
492 (*list2
)->type
== ET8EK8_REGLIST_MODE
)
495 /* Descending width. */
496 if ((*list1
)->mode
.window_width
> (*list2
)->mode
.window_width
)
498 if ((*list1
)->mode
.window_width
< (*list2
)->mode
.window_width
)
501 if ((*list1
)->mode
.window_height
> (*list2
)->mode
.window_height
)
503 if ((*list1
)->mode
.window_height
< (*list2
)->mode
.window_height
)
509 static int et8ek8_reglist_import(struct i2c_client
*client
,
510 struct et8ek8_meta_reglist
*meta
)
514 dev_info(&client
->dev
, "meta_reglist version %s\n", meta
->version
);
516 while (meta
->reglist
[nlists
].ptr
)
522 sort(&meta
->reglist
[0].ptr
, nlists
, sizeof(meta
->reglist
[0].ptr
),
523 et8ek8_reglist_cmp
, NULL
);
529 struct et8ek8_reglist
*list
;
531 list
= meta
->reglist
[nlists
].ptr
;
533 dev_dbg(&client
->dev
,
534 "%s: type %d\tw %d\th %d\tfmt %x\tival %d/%d\tptr %p\n",
537 list
->mode
.window_width
, list
->mode
.window_height
,
538 list
->mode
.bus_format
,
539 list
->mode
.timeperframe
.numerator
,
540 list
->mode
.timeperframe
.denominator
,
541 (void *)meta
->reglist
[nlists
].ptr
);
549 /* Called to change the V4L2 gain control value. This function
550 * rounds and clamps the given value and updates the V4L2 control value.
551 * If power is on, also updates the sensor analog and digital gains.
552 * gain is in 0.1 EV (exposure value) units.
554 static int et8ek8_set_gain(struct et8ek8_sensor
*sensor
, s32 gain
)
556 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
557 struct et8ek8_gain
new;
560 new = et8ek8_gain_table
[gain
];
562 /* FIXME: optimise I2C writes! */
563 r
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
,
564 0x124a, new.analog
>> 8);
567 r
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
,
568 0x1249, new.analog
& 0xff);
572 r
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
,
573 0x124d, new.digital
>> 8);
576 r
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
,
577 0x124c, new.digital
& 0xff);
582 static int et8ek8_set_test_pattern(struct et8ek8_sensor
*sensor
, s32 mode
)
584 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
585 int cbh_mode
, cbv_mode
, tp_mode
, din_sw
, r1420
, rval
;
587 /* Values for normal mode */
595 /* Test pattern mode */
603 tp_mode
= mode
- 4 + 3;
610 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x111B,
615 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1121,
620 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1124,
625 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x112C, din_sw
);
629 return et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1420, r1420
);
632 /* -----------------------------------------------------------------------------
636 static int et8ek8_set_ctrl(struct v4l2_ctrl
*ctrl
)
638 struct et8ek8_sensor
*sensor
=
639 container_of(ctrl
->handler
, struct et8ek8_sensor
, ctrl_handler
);
643 return et8ek8_set_gain(sensor
, ctrl
->val
);
645 case V4L2_CID_EXPOSURE
:
647 struct i2c_client
*client
=
648 v4l2_get_subdevdata(&sensor
->subdev
);
650 return et8ek8_i2c_write_reg(client
, ET8EK8_REG_16BIT
, 0x1243,
654 case V4L2_CID_TEST_PATTERN
:
655 return et8ek8_set_test_pattern(sensor
, ctrl
->val
);
657 case V4L2_CID_PIXEL_RATE
:
665 static const struct v4l2_ctrl_ops et8ek8_ctrl_ops
= {
666 .s_ctrl
= et8ek8_set_ctrl
,
669 static const char * const et8ek8_test_pattern_menu
[] = {
672 "Horizontal colorbar",
675 "Small vertical colorbar",
676 "Small horizontal colorbar",
681 static int et8ek8_init_controls(struct et8ek8_sensor
*sensor
)
685 v4l2_ctrl_handler_init(&sensor
->ctrl_handler
, 4);
688 v4l2_ctrl_new_std(&sensor
->ctrl_handler
, &et8ek8_ctrl_ops
,
689 V4L2_CID_GAIN
, 0, ARRAY_SIZE(et8ek8_gain_table
) - 1,
692 max_rows
= sensor
->current_reglist
->mode
.max_exp
;
694 u32 min
= 1, max
= max_rows
;
697 v4l2_ctrl_new_std(&sensor
->ctrl_handler
,
698 &et8ek8_ctrl_ops
, V4L2_CID_EXPOSURE
,
702 /* V4L2_CID_PIXEL_RATE */
704 v4l2_ctrl_new_std(&sensor
->ctrl_handler
, &et8ek8_ctrl_ops
,
705 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
707 /* V4L2_CID_TEST_PATTERN */
708 v4l2_ctrl_new_std_menu_items(&sensor
->ctrl_handler
,
709 &et8ek8_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
710 ARRAY_SIZE(et8ek8_test_pattern_menu
) - 1,
711 0, 0, et8ek8_test_pattern_menu
);
713 if (sensor
->ctrl_handler
.error
)
714 return sensor
->ctrl_handler
.error
;
716 sensor
->subdev
.ctrl_handler
= &sensor
->ctrl_handler
;
721 static void et8ek8_update_controls(struct et8ek8_sensor
*sensor
)
723 struct v4l2_ctrl
*ctrl
;
724 struct et8ek8_mode
*mode
= &sensor
->current_reglist
->mode
;
726 u32 min
, max
, pixel_rate
;
727 static const int S
= 8;
729 ctrl
= sensor
->exposure
;
735 * Calculate average pixel clock per line. Assume buffers can spread
736 * the data over horizontal blanking time. Rounding upwards.
737 * Formula taken from stock Nokia N900 kernel.
739 pixel_rate
= ((mode
->pixel_clock
+ (1 << S
) - 1) >> S
) + mode
->width
;
740 pixel_rate
= mode
->window_width
* (pixel_rate
- 1) / mode
->width
;
742 __v4l2_ctrl_modify_range(ctrl
, min
, max
, min
, max
);
743 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate
, pixel_rate
<< S
);
746 static int et8ek8_configure(struct et8ek8_sensor
*sensor
)
748 struct v4l2_subdev
*subdev
= &sensor
->subdev
;
749 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
752 rval
= et8ek8_i2c_write_regs(client
, sensor
->current_reglist
->regs
);
756 /* Controls set while the power to the sensor is turned off are saved
757 * but not applied to the hardware. Now that we're about to start
758 * streaming apply all the current values to the hardware.
760 rval
= v4l2_ctrl_handler_setup(&sensor
->ctrl_handler
);
767 dev_err(&client
->dev
, "sensor configuration failed\n");
772 static int et8ek8_stream_on(struct et8ek8_sensor
*sensor
)
774 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
776 return et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1252, 0xb0);
779 static int et8ek8_stream_off(struct et8ek8_sensor
*sensor
)
781 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
783 return et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1252, 0x30);
786 static int et8ek8_s_stream(struct v4l2_subdev
*subdev
, int streaming
)
788 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
792 return et8ek8_stream_off(sensor
);
794 ret
= et8ek8_configure(sensor
);
798 return et8ek8_stream_on(sensor
);
801 /* --------------------------------------------------------------------------
802 * V4L2 subdev operations
805 static int et8ek8_power_off(struct et8ek8_sensor
*sensor
)
807 gpiod_set_value(sensor
->reset
, 0);
810 clk_disable_unprepare(sensor
->ext_clk
);
812 return regulator_disable(sensor
->vana
);
815 static int et8ek8_power_on(struct et8ek8_sensor
*sensor
)
817 struct v4l2_subdev
*subdev
= &sensor
->subdev
;
818 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
819 unsigned int xclk_freq
;
822 rval
= regulator_enable(sensor
->vana
);
824 dev_err(&client
->dev
, "failed to enable vana regulator\n");
828 if (sensor
->current_reglist
)
829 xclk_freq
= sensor
->current_reglist
->mode
.ext_clock
;
831 xclk_freq
= sensor
->xclk_freq
;
833 rval
= clk_set_rate(sensor
->ext_clk
, xclk_freq
);
835 dev_err(&client
->dev
, "unable to set extclk clock freq to %u\n",
839 rval
= clk_prepare_enable(sensor
->ext_clk
);
841 dev_err(&client
->dev
, "failed to enable extclk\n");
848 udelay(10); /* I wish this is a good value */
850 gpiod_set_value(sensor
->reset
, 1);
852 msleep(5000 * 1000 / xclk_freq
+ 1); /* Wait 5000 cycles */
854 rval
= et8ek8_i2c_reglist_find_write(client
, &meta_reglist
,
855 ET8EK8_REGLIST_POWERON
);
860 rval
= et8ek8_i2c_read_reg(client
, ET8EK8_REG_8BIT
, 0x1263, &val
);
863 #if USE_CRC /* TODO get crc setting from DT */
868 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x1263, val
);
875 et8ek8_power_off(sensor
);
880 /* --------------------------------------------------------------------------
881 * V4L2 subdev video operations
884 static int et8ek8_enum_mbus_code(struct v4l2_subdev
*subdev
,
885 struct v4l2_subdev_pad_config
*cfg
,
886 struct v4l2_subdev_mbus_code_enum
*code
)
888 struct et8ek8_reglist
**list
=
889 et8ek8_reglist_first(&meta_reglist
);
890 u32 pixelformat
[MAX_FMTS
];
891 int npixelformat
= 0;
893 if (code
->index
>= MAX_FMTS
)
896 for (; *list
; list
++) {
897 struct et8ek8_mode
*mode
= &(*list
)->mode
;
900 if ((*list
)->type
!= ET8EK8_REGLIST_MODE
)
903 for (i
= 0; i
< npixelformat
; i
++) {
904 if (pixelformat
[i
] == mode
->bus_format
)
907 if (i
!= npixelformat
)
910 if (code
->index
== npixelformat
) {
911 code
->code
= mode
->bus_format
;
915 pixelformat
[npixelformat
] = mode
->bus_format
;
922 static int et8ek8_enum_frame_size(struct v4l2_subdev
*subdev
,
923 struct v4l2_subdev_pad_config
*cfg
,
924 struct v4l2_subdev_frame_size_enum
*fse
)
926 struct et8ek8_reglist
**list
=
927 et8ek8_reglist_first(&meta_reglist
);
928 struct v4l2_mbus_framefmt format
;
929 int cmp_width
= INT_MAX
;
930 int cmp_height
= INT_MAX
;
931 int index
= fse
->index
;
933 for (; *list
; list
++) {
934 if ((*list
)->type
!= ET8EK8_REGLIST_MODE
)
937 et8ek8_reglist_to_mbus(*list
, &format
);
938 if (fse
->code
!= format
.code
)
941 /* Assume that the modes are grouped by frame size. */
942 if (format
.width
== cmp_width
&& format
.height
== cmp_height
)
945 cmp_width
= format
.width
;
946 cmp_height
= format
.height
;
949 fse
->min_width
= format
.width
;
950 fse
->min_height
= format
.height
;
951 fse
->max_width
= format
.width
;
952 fse
->max_height
= format
.height
;
960 static int et8ek8_enum_frame_ival(struct v4l2_subdev
*subdev
,
961 struct v4l2_subdev_pad_config
*cfg
,
962 struct v4l2_subdev_frame_interval_enum
*fie
)
964 struct et8ek8_reglist
**list
=
965 et8ek8_reglist_first(&meta_reglist
);
966 struct v4l2_mbus_framefmt format
;
967 int index
= fie
->index
;
969 for (; *list
; list
++) {
970 struct et8ek8_mode
*mode
= &(*list
)->mode
;
972 if ((*list
)->type
!= ET8EK8_REGLIST_MODE
)
975 et8ek8_reglist_to_mbus(*list
, &format
);
976 if (fie
->code
!= format
.code
)
979 if (fie
->width
!= format
.width
|| fie
->height
!= format
.height
)
983 fie
->interval
= mode
->timeperframe
;
991 static struct v4l2_mbus_framefmt
*
992 __et8ek8_get_pad_format(struct et8ek8_sensor
*sensor
,
993 struct v4l2_subdev_pad_config
*cfg
,
994 unsigned int pad
, enum v4l2_subdev_format_whence which
)
997 case V4L2_SUBDEV_FORMAT_TRY
:
998 return v4l2_subdev_get_try_format(&sensor
->subdev
, cfg
, pad
);
999 case V4L2_SUBDEV_FORMAT_ACTIVE
:
1000 return &sensor
->format
;
1006 static int et8ek8_get_pad_format(struct v4l2_subdev
*subdev
,
1007 struct v4l2_subdev_pad_config
*cfg
,
1008 struct v4l2_subdev_format
*fmt
)
1010 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1011 struct v4l2_mbus_framefmt
*format
;
1013 format
= __et8ek8_get_pad_format(sensor
, cfg
, fmt
->pad
, fmt
->which
);
1017 fmt
->format
= *format
;
1022 static int et8ek8_set_pad_format(struct v4l2_subdev
*subdev
,
1023 struct v4l2_subdev_pad_config
*cfg
,
1024 struct v4l2_subdev_format
*fmt
)
1026 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1027 struct v4l2_mbus_framefmt
*format
;
1028 struct et8ek8_reglist
*reglist
;
1030 format
= __et8ek8_get_pad_format(sensor
, cfg
, fmt
->pad
, fmt
->which
);
1034 reglist
= et8ek8_reglist_find_mode_fmt(&meta_reglist
, &fmt
->format
);
1035 et8ek8_reglist_to_mbus(reglist
, &fmt
->format
);
1036 *format
= fmt
->format
;
1038 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1039 sensor
->current_reglist
= reglist
;
1040 et8ek8_update_controls(sensor
);
1046 static int et8ek8_get_frame_interval(struct v4l2_subdev
*subdev
,
1047 struct v4l2_subdev_frame_interval
*fi
)
1049 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1051 memset(fi
, 0, sizeof(*fi
));
1052 fi
->interval
= sensor
->current_reglist
->mode
.timeperframe
;
1057 static int et8ek8_set_frame_interval(struct v4l2_subdev
*subdev
,
1058 struct v4l2_subdev_frame_interval
*fi
)
1060 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1061 struct et8ek8_reglist
*reglist
;
1063 reglist
= et8ek8_reglist_find_mode_ival(&meta_reglist
,
1064 sensor
->current_reglist
,
1070 if (sensor
->current_reglist
->mode
.ext_clock
!= reglist
->mode
.ext_clock
)
1073 sensor
->current_reglist
= reglist
;
1074 et8ek8_update_controls(sensor
);
1079 static int et8ek8_g_priv_mem(struct v4l2_subdev
*subdev
)
1081 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1082 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1083 unsigned int length
= ET8EK8_PRIV_MEM_SIZE
;
1084 unsigned int offset
= 0;
1085 u8
*ptr
= sensor
->priv_mem
;
1088 /* Read the EEPROM window-by-window, each window 8 bytes */
1090 u8 buffer
[PRIV_MEM_WIN_SIZE
];
1095 /* Set the current window */
1096 rval
= et8ek8_i2c_write_reg(client
, ET8EK8_REG_8BIT
, 0x0001,
1097 0xe0 | (offset
>> 3));
1101 /* Wait for status bit */
1102 for (i
= 0; i
< 1000; ++i
) {
1105 rval
= et8ek8_i2c_read_reg(client
, ET8EK8_REG_8BIT
,
1109 if (!(status
& 0x08))
1111 usleep_range(1000, 2000);
1117 /* Read window, 8 bytes at once, and copy to user space */
1118 ofs
= offset
& 0x07; /* Offset within this window */
1119 bytes
= length
+ ofs
> 8 ? 8-ofs
: length
;
1120 msg
.addr
= client
->addr
;
1124 ofs
+= PRIV_MEM_START_REG
;
1125 buffer
[0] = (u8
)(ofs
>> 8);
1126 buffer
[1] = (u8
)(ofs
& 0xFF);
1128 rval
= i2c_transfer(client
->adapter
, &msg
, 1);
1132 mdelay(ET8EK8_I2C_DELAY
);
1133 msg
.addr
= client
->addr
;
1135 msg
.flags
= I2C_M_RD
;
1137 memset(buffer
, 0, sizeof(buffer
));
1139 rval
= i2c_transfer(client
->adapter
, &msg
, 1);
1144 memcpy(ptr
, buffer
, bytes
);
1149 } while (length
> 0);
1154 static int et8ek8_dev_init(struct v4l2_subdev
*subdev
)
1156 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1157 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1158 int rval
, rev_l
, rev_h
;
1160 rval
= et8ek8_power_on(sensor
);
1162 dev_err(&client
->dev
, "could not power on\n");
1166 rval
= et8ek8_i2c_read_reg(client
, ET8EK8_REG_8BIT
,
1167 REG_REVISION_NUMBER_L
, &rev_l
);
1169 rval
= et8ek8_i2c_read_reg(client
, ET8EK8_REG_8BIT
,
1170 REG_REVISION_NUMBER_H
, &rev_h
);
1172 dev_err(&client
->dev
, "no et8ek8 sensor detected\n");
1176 sensor
->version
= (rev_h
<< 8) + rev_l
;
1177 if (sensor
->version
!= ET8EK8_REV_1
&& sensor
->version
!= ET8EK8_REV_2
)
1178 dev_info(&client
->dev
,
1179 "unknown version 0x%x detected, continuing anyway\n",
1182 rval
= et8ek8_reglist_import(client
, &meta_reglist
);
1184 dev_err(&client
->dev
,
1185 "invalid register list %s, import failed\n",
1190 sensor
->current_reglist
= et8ek8_reglist_find_type(&meta_reglist
,
1191 ET8EK8_REGLIST_MODE
);
1192 if (!sensor
->current_reglist
) {
1193 dev_err(&client
->dev
,
1194 "invalid register list %s, no mode found\n",
1200 et8ek8_reglist_to_mbus(sensor
->current_reglist
, &sensor
->format
);
1202 rval
= et8ek8_i2c_reglist_find_write(client
, &meta_reglist
,
1203 ET8EK8_REGLIST_POWERON
);
1205 dev_err(&client
->dev
,
1206 "invalid register list %s, no POWERON mode found\n",
1210 rval
= et8ek8_stream_on(sensor
); /* Needed to be able to read EEPROM */
1213 rval
= et8ek8_g_priv_mem(subdev
);
1215 dev_warn(&client
->dev
,
1216 "can not read OTP (EEPROM) memory from sensor\n");
1217 rval
= et8ek8_stream_off(sensor
);
1221 rval
= et8ek8_power_off(sensor
);
1228 et8ek8_power_off(sensor
);
1233 /* --------------------------------------------------------------------------
1237 et8ek8_priv_mem_read(struct device
*dev
, struct device_attribute
*attr
,
1240 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
1241 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1243 #if PAGE_SIZE < ET8EK8_PRIV_MEM_SIZE
1244 #error PAGE_SIZE too small!
1247 memcpy(buf
, sensor
->priv_mem
, ET8EK8_PRIV_MEM_SIZE
);
1249 return ET8EK8_PRIV_MEM_SIZE
;
1251 static DEVICE_ATTR(priv_mem
, 0444, et8ek8_priv_mem_read
, NULL
);
1253 /* --------------------------------------------------------------------------
1254 * V4L2 subdev core operations
1258 et8ek8_registered(struct v4l2_subdev
*subdev
)
1260 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1261 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1264 dev_dbg(&client
->dev
, "registered!");
1266 rval
= device_create_file(&client
->dev
, &dev_attr_priv_mem
);
1268 dev_err(&client
->dev
, "could not register sysfs entry\n");
1272 rval
= et8ek8_dev_init(subdev
);
1276 rval
= et8ek8_init_controls(sensor
);
1278 dev_err(&client
->dev
, "controls initialization failed\n");
1282 __et8ek8_get_pad_format(sensor
, NULL
, 0, V4L2_SUBDEV_FORMAT_ACTIVE
);
1287 device_remove_file(&client
->dev
, &dev_attr_priv_mem
);
1292 static int __et8ek8_set_power(struct et8ek8_sensor
*sensor
, bool on
)
1294 return on
? et8ek8_power_on(sensor
) : et8ek8_power_off(sensor
);
1297 static int et8ek8_set_power(struct v4l2_subdev
*subdev
, int on
)
1299 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1302 mutex_lock(&sensor
->power_lock
);
1304 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
1305 * update the power state.
1307 if (sensor
->power_count
== !on
) {
1308 ret
= __et8ek8_set_power(sensor
, !!on
);
1313 /* Update the power count. */
1314 sensor
->power_count
+= on
? 1 : -1;
1315 WARN_ON(sensor
->power_count
< 0);
1318 mutex_unlock(&sensor
->power_lock
);
1323 static int et8ek8_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1325 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(sd
);
1326 struct v4l2_mbus_framefmt
*format
;
1327 struct et8ek8_reglist
*reglist
;
1329 reglist
= et8ek8_reglist_find_type(&meta_reglist
, ET8EK8_REGLIST_MODE
);
1330 format
= __et8ek8_get_pad_format(sensor
, fh
->pad
, 0,
1331 V4L2_SUBDEV_FORMAT_TRY
);
1332 et8ek8_reglist_to_mbus(reglist
, format
);
1334 return et8ek8_set_power(sd
, true);
1337 static int et8ek8_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1339 return et8ek8_set_power(sd
, false);
1342 static const struct v4l2_subdev_video_ops et8ek8_video_ops
= {
1343 .s_stream
= et8ek8_s_stream
,
1344 .g_frame_interval
= et8ek8_get_frame_interval
,
1345 .s_frame_interval
= et8ek8_set_frame_interval
,
1348 static const struct v4l2_subdev_core_ops et8ek8_core_ops
= {
1349 .s_power
= et8ek8_set_power
,
1352 static const struct v4l2_subdev_pad_ops et8ek8_pad_ops
= {
1353 .enum_mbus_code
= et8ek8_enum_mbus_code
,
1354 .enum_frame_size
= et8ek8_enum_frame_size
,
1355 .enum_frame_interval
= et8ek8_enum_frame_ival
,
1356 .get_fmt
= et8ek8_get_pad_format
,
1357 .set_fmt
= et8ek8_set_pad_format
,
1360 static const struct v4l2_subdev_ops et8ek8_ops
= {
1361 .core
= &et8ek8_core_ops
,
1362 .video
= &et8ek8_video_ops
,
1363 .pad
= &et8ek8_pad_ops
,
1366 static const struct v4l2_subdev_internal_ops et8ek8_internal_ops
= {
1367 .registered
= et8ek8_registered
,
1368 .open
= et8ek8_open
,
1369 .close
= et8ek8_close
,
1372 /* --------------------------------------------------------------------------
1375 static int __maybe_unused
et8ek8_suspend(struct device
*dev
)
1377 struct i2c_client
*client
= to_i2c_client(dev
);
1378 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1379 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1381 if (!sensor
->power_count
)
1384 return __et8ek8_set_power(sensor
, false);
1387 static int __maybe_unused
et8ek8_resume(struct device
*dev
)
1389 struct i2c_client
*client
= to_i2c_client(dev
);
1390 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1391 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1393 if (!sensor
->power_count
)
1396 return __et8ek8_set_power(sensor
, true);
1399 static int et8ek8_probe(struct i2c_client
*client
)
1401 struct et8ek8_sensor
*sensor
;
1402 struct device
*dev
= &client
->dev
;
1405 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
1409 sensor
->reset
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
1410 if (IS_ERR(sensor
->reset
)) {
1411 dev_dbg(&client
->dev
, "could not request reset gpio\n");
1412 return PTR_ERR(sensor
->reset
);
1415 sensor
->vana
= devm_regulator_get(dev
, "vana");
1416 if (IS_ERR(sensor
->vana
)) {
1417 dev_err(&client
->dev
, "could not get regulator for vana\n");
1418 return PTR_ERR(sensor
->vana
);
1421 sensor
->ext_clk
= devm_clk_get(dev
, NULL
);
1422 if (IS_ERR(sensor
->ext_clk
)) {
1423 dev_err(&client
->dev
, "could not get clock\n");
1424 return PTR_ERR(sensor
->ext_clk
);
1427 ret
= of_property_read_u32(dev
->of_node
, "clock-frequency",
1428 &sensor
->xclk_freq
);
1430 dev_warn(dev
, "can't get clock-frequency\n");
1434 mutex_init(&sensor
->power_lock
);
1436 v4l2_i2c_subdev_init(&sensor
->subdev
, client
, &et8ek8_ops
);
1437 sensor
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1438 sensor
->subdev
.internal_ops
= &et8ek8_internal_ops
;
1440 sensor
->subdev
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1441 sensor
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1442 ret
= media_entity_pads_init(&sensor
->subdev
.entity
, 1, &sensor
->pad
);
1444 dev_err(&client
->dev
, "media entity init failed!\n");
1448 ret
= v4l2_async_register_subdev_sensor_common(&sensor
->subdev
);
1452 dev_dbg(dev
, "initialized!\n");
1457 media_entity_cleanup(&sensor
->subdev
.entity
);
1459 mutex_destroy(&sensor
->power_lock
);
1463 static int __exit
et8ek8_remove(struct i2c_client
*client
)
1465 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1466 struct et8ek8_sensor
*sensor
= to_et8ek8_sensor(subdev
);
1468 if (sensor
->power_count
) {
1470 et8ek8_power_off(sensor
);
1471 sensor
->power_count
= 0;
1474 v4l2_device_unregister_subdev(&sensor
->subdev
);
1475 device_remove_file(&client
->dev
, &dev_attr_priv_mem
);
1476 v4l2_ctrl_handler_free(&sensor
->ctrl_handler
);
1477 v4l2_async_unregister_subdev(&sensor
->subdev
);
1478 media_entity_cleanup(&sensor
->subdev
.entity
);
1479 mutex_destroy(&sensor
->power_lock
);
1484 static const struct of_device_id et8ek8_of_table
[] = {
1485 { .compatible
= "toshiba,et8ek8" },
1488 MODULE_DEVICE_TABLE(of
, et8ek8_of_table
);
1490 static const struct i2c_device_id et8ek8_id_table
[] = {
1494 MODULE_DEVICE_TABLE(i2c
, et8ek8_id_table
);
1496 static const struct dev_pm_ops et8ek8_pm_ops
= {
1497 SET_SYSTEM_SLEEP_PM_OPS(et8ek8_suspend
, et8ek8_resume
)
1500 static struct i2c_driver et8ek8_i2c_driver
= {
1502 .name
= ET8EK8_NAME
,
1503 .pm
= &et8ek8_pm_ops
,
1504 .of_match_table
= et8ek8_of_table
,
1506 .probe_new
= et8ek8_probe
,
1507 .remove
= __exit_p(et8ek8_remove
),
1508 .id_table
= et8ek8_id_table
,
1511 module_i2c_driver(et8ek8_i2c_driver
);
1513 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>, Pavel Machek <pavel@ucw.cz");
1514 MODULE_DESCRIPTION("Toshiba ET8EK8 camera sensor driver");
1515 MODULE_LICENSE("GPL");