]>
Commit | Line | Data |
---|---|---|
22db44cb | 1 | /* |
cb7a01ac | 2 | * drivers/media/i2c/as3645a.c - AS3645A and LM3555 flash controllers driver |
22db44cb LP |
3 | * |
4 | * Copyright (C) 2008-2011 Nokia Corporation | |
5 | * Copyright (c) 2011, Intel Corporation. | |
6 | * | |
7 | * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License | |
11 | * version 2 as published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
22db44cb LP |
18 | * TODO: |
19 | * - Check hardware FSTROBE control when sensor driver add support for this | |
20 | * | |
21 | */ | |
22 | ||
23 | #include <linux/delay.h> | |
24 | #include <linux/i2c.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/mutex.h> | |
36be126c | 27 | #include <linux/slab.h> |
22db44cb | 28 | |
b5dcee22 | 29 | #include <media/i2c/as3645a.h> |
22db44cb LP |
30 | #include <media/v4l2-ctrls.h> |
31 | #include <media/v4l2-device.h> | |
32 | ||
33 | #define AS_TIMER_MS_TO_CODE(t) (((t) - 100) / 50) | |
34 | #define AS_TIMER_CODE_TO_MS(c) (50 * (c) + 100) | |
35 | ||
36 | /* Register definitions */ | |
37 | ||
38 | /* Read-only Design info register: Reset state: xxxx 0001 */ | |
39 | #define AS_DESIGN_INFO_REG 0x00 | |
40 | #define AS_DESIGN_INFO_FACTORY(x) (((x) >> 4)) | |
41 | #define AS_DESIGN_INFO_MODEL(x) ((x) & 0x0f) | |
42 | ||
43 | /* Read-only Version control register: Reset state: 0000 0000 | |
44 | * for first engineering samples | |
45 | */ | |
46 | #define AS_VERSION_CONTROL_REG 0x01 | |
47 | #define AS_VERSION_CONTROL_RFU(x) (((x) >> 4)) | |
48 | #define AS_VERSION_CONTROL_VERSION(x) ((x) & 0x0f) | |
49 | ||
50 | /* Read / Write (Indicator and timer register): Reset state: 0000 1111 */ | |
51 | #define AS_INDICATOR_AND_TIMER_REG 0x02 | |
52 | #define AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT 0 | |
53 | #define AS_INDICATOR_AND_TIMER_VREF_SHIFT 4 | |
54 | #define AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT 6 | |
55 | ||
56 | /* Read / Write (Current set register): Reset state: 0110 1001 */ | |
57 | #define AS_CURRENT_SET_REG 0x03 | |
58 | #define AS_CURRENT_ASSIST_LIGHT_SHIFT 0 | |
59 | #define AS_CURRENT_LED_DET_ON (1 << 3) | |
60 | #define AS_CURRENT_FLASH_CURRENT_SHIFT 4 | |
61 | ||
62 | /* Read / Write (Control register): Reset state: 1011 0100 */ | |
63 | #define AS_CONTROL_REG 0x04 | |
64 | #define AS_CONTROL_MODE_SETTING_SHIFT 0 | |
65 | #define AS_CONTROL_STROBE_ON (1 << 2) | |
66 | #define AS_CONTROL_OUT_ON (1 << 3) | |
67 | #define AS_CONTROL_EXT_TORCH_ON (1 << 4) | |
68 | #define AS_CONTROL_STROBE_TYPE_EDGE (0 << 5) | |
69 | #define AS_CONTROL_STROBE_TYPE_LEVEL (1 << 5) | |
70 | #define AS_CONTROL_COIL_PEAK_SHIFT 6 | |
71 | ||
72 | /* Read only (D3 is read / write) (Fault and info): Reset state: 0000 x000 */ | |
73 | #define AS_FAULT_INFO_REG 0x05 | |
74 | #define AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT (1 << 1) | |
75 | #define AS_FAULT_INFO_INDICATOR_LED (1 << 2) | |
76 | #define AS_FAULT_INFO_LED_AMOUNT (1 << 3) | |
77 | #define AS_FAULT_INFO_TIMEOUT (1 << 4) | |
78 | #define AS_FAULT_INFO_OVER_TEMPERATURE (1 << 5) | |
79 | #define AS_FAULT_INFO_SHORT_CIRCUIT (1 << 6) | |
80 | #define AS_FAULT_INFO_OVER_VOLTAGE (1 << 7) | |
81 | ||
82 | /* Boost register */ | |
83 | #define AS_BOOST_REG 0x0d | |
84 | #define AS_BOOST_CURRENT_DISABLE (0 << 0) | |
85 | #define AS_BOOST_CURRENT_ENABLE (1 << 0) | |
86 | ||
87 | /* Password register is used to unlock boost register writing */ | |
88 | #define AS_PASSWORD_REG 0x0f | |
89 | #define AS_PASSWORD_UNLOCK_VALUE 0x55 | |
90 | ||
91 | enum as_mode { | |
92 | AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT, | |
93 | AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT, | |
94 | AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT, | |
95 | AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT, | |
96 | }; | |
97 | ||
98 | /* | |
99 | * struct as3645a | |
100 | * | |
101 | * @subdev: V4L2 subdev | |
102 | * @pdata: Flash platform data | |
103 | * @power_lock: Protects power_count | |
104 | * @power_count: Power reference count | |
105 | * @led_mode: V4L2 flash LED mode | |
106 | * @timeout: Flash timeout in microseconds | |
107 | * @flash_current: Flash current (0=200mA ... 15=500mA). Maximum | |
108 | * values are 400mA for two LEDs and 500mA for one LED. | |
109 | * @assist_current: Torch/Assist light current (0=20mA, 1=40mA ... 7=160mA) | |
110 | * @indicator_current: Indicator LED current (0=0mA, 1=2.5mA ... 4=10mA) | |
111 | * @strobe_source: Flash strobe source (software or external) | |
112 | */ | |
113 | struct as3645a { | |
114 | struct v4l2_subdev subdev; | |
115 | const struct as3645a_platform_data *pdata; | |
116 | ||
117 | struct mutex power_lock; | |
118 | int power_count; | |
119 | ||
120 | /* Controls */ | |
121 | struct v4l2_ctrl_handler ctrls; | |
122 | ||
123 | enum v4l2_flash_led_mode led_mode; | |
124 | unsigned int timeout; | |
125 | u8 flash_current; | |
126 | u8 assist_current; | |
127 | u8 indicator_current; | |
128 | enum v4l2_flash_strobe_source strobe_source; | |
129 | }; | |
130 | ||
131 | #define to_as3645a(sd) container_of(sd, struct as3645a, subdev) | |
132 | ||
133 | /* Return negative errno else zero on success */ | |
134 | static int as3645a_write(struct as3645a *flash, u8 addr, u8 val) | |
135 | { | |
136 | struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); | |
137 | int rval; | |
138 | ||
139 | rval = i2c_smbus_write_byte_data(client, addr, val); | |
140 | ||
141 | dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val, | |
142 | rval < 0 ? "fail" : "ok"); | |
143 | ||
144 | return rval; | |
145 | } | |
146 | ||
147 | /* Return negative errno else a data byte received from the device. */ | |
148 | static int as3645a_read(struct as3645a *flash, u8 addr) | |
149 | { | |
150 | struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); | |
151 | int rval; | |
152 | ||
153 | rval = i2c_smbus_read_byte_data(client, addr); | |
154 | ||
155 | dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval, | |
156 | rval < 0 ? "fail" : "ok"); | |
157 | ||
158 | return rval; | |
159 | } | |
160 | ||
161 | /* ----------------------------------------------------------------------------- | |
162 | * Hardware configuration and trigger | |
163 | */ | |
164 | ||
165 | /* | |
166 | * as3645a_set_config - Set flash configuration registers | |
167 | * @flash: The flash | |
168 | * | |
169 | * Configure the hardware with flash, assist and indicator currents, as well as | |
170 | * flash timeout. | |
171 | * | |
172 | * Return 0 on success, or a negative error code if an I2C communication error | |
173 | * occurred. | |
174 | */ | |
175 | static int as3645a_set_config(struct as3645a *flash) | |
176 | { | |
177 | int ret; | |
178 | u8 val; | |
179 | ||
180 | val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT) | |
181 | | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT) | |
182 | | AS_CURRENT_LED_DET_ON; | |
183 | ||
184 | ret = as3645a_write(flash, AS_CURRENT_SET_REG, val); | |
185 | if (ret < 0) | |
186 | return ret; | |
187 | ||
188 | val = AS_TIMER_MS_TO_CODE(flash->timeout / 1000) | |
189 | << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT; | |
190 | ||
191 | val |= (flash->pdata->vref << AS_INDICATOR_AND_TIMER_VREF_SHIFT) | |
192 | | ((flash->indicator_current ? flash->indicator_current - 1 : 0) | |
193 | << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT); | |
194 | ||
195 | return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val); | |
196 | } | |
197 | ||
198 | /* | |
199 | * as3645a_set_control - Set flash control register | |
200 | * @flash: The flash | |
201 | * @mode: Desired output mode | |
202 | * @on: Desired output state | |
203 | * | |
204 | * Configure the hardware with output mode and state. | |
205 | * | |
206 | * Return 0 on success, or a negative error code if an I2C communication error | |
207 | * occurred. | |
208 | */ | |
209 | static int | |
210 | as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on) | |
211 | { | |
212 | u8 reg; | |
213 | ||
214 | /* Configure output parameters and operation mode. */ | |
215 | reg = (flash->pdata->peak << AS_CONTROL_COIL_PEAK_SHIFT) | |
216 | | (on ? AS_CONTROL_OUT_ON : 0) | |
217 | | mode; | |
218 | ||
219 | if (flash->led_mode == V4L2_FLASH_LED_MODE_FLASH && | |
220 | flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL) { | |
221 | reg |= AS_CONTROL_STROBE_TYPE_LEVEL | |
222 | | AS_CONTROL_STROBE_ON; | |
223 | } | |
224 | ||
225 | return as3645a_write(flash, AS_CONTROL_REG, reg); | |
226 | } | |
227 | ||
228 | /* | |
229 | * as3645a_set_output - Configure output and operation mode | |
230 | * @flash: Flash controller | |
231 | * @strobe: Strobe the flash (only valid in flash mode) | |
232 | * | |
233 | * Turn the LEDs output on/off and set the operation mode based on the current | |
234 | * parameters. | |
235 | * | |
236 | * The AS3645A can't control the indicator LED independently of the flash/torch | |
237 | * LED. If the flash controller is in V4L2_FLASH_LED_MODE_NONE mode, set the | |
238 | * chip to indicator mode. Otherwise set it to assist light (torch) or flash | |
239 | * mode. | |
240 | * | |
241 | * In indicator and assist modes, turn the output on/off based on the indicator | |
242 | * and torch currents. In software strobe flash mode, turn the output on/off | |
243 | * based on the strobe parameter. | |
244 | */ | |
245 | static int as3645a_set_output(struct as3645a *flash, bool strobe) | |
246 | { | |
247 | enum as_mode mode; | |
248 | bool on; | |
249 | ||
250 | switch (flash->led_mode) { | |
251 | case V4L2_FLASH_LED_MODE_NONE: | |
252 | on = flash->indicator_current != 0; | |
253 | mode = AS_MODE_INDICATOR; | |
254 | break; | |
255 | case V4L2_FLASH_LED_MODE_TORCH: | |
256 | on = true; | |
257 | mode = AS_MODE_ASSIST; | |
258 | break; | |
259 | case V4L2_FLASH_LED_MODE_FLASH: | |
260 | on = strobe; | |
261 | mode = AS_MODE_FLASH; | |
262 | break; | |
263 | default: | |
264 | BUG(); | |
265 | } | |
266 | ||
267 | /* Configure output parameters and operation mode. */ | |
268 | return as3645a_set_control(flash, mode, on); | |
269 | } | |
270 | ||
271 | /* ----------------------------------------------------------------------------- | |
272 | * V4L2 controls | |
273 | */ | |
274 | ||
275 | static int as3645a_is_active(struct as3645a *flash) | |
276 | { | |
277 | int ret; | |
278 | ||
279 | ret = as3645a_read(flash, AS_CONTROL_REG); | |
280 | return ret < 0 ? ret : !!(ret & AS_CONTROL_OUT_ON); | |
281 | } | |
282 | ||
283 | static int as3645a_read_fault(struct as3645a *flash) | |
284 | { | |
285 | struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); | |
286 | int rval; | |
287 | ||
288 | /* NOTE: reading register clear fault status */ | |
289 | rval = as3645a_read(flash, AS_FAULT_INFO_REG); | |
290 | if (rval < 0) | |
291 | return rval; | |
292 | ||
293 | if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT) | |
294 | dev_dbg(&client->dev, "Inductor Peak limit fault\n"); | |
295 | ||
296 | if (rval & AS_FAULT_INFO_INDICATOR_LED) | |
4ac29941 AS |
297 | dev_dbg(&client->dev, |
298 | "Indicator LED fault: Short circuit or open loop\n"); | |
22db44cb LP |
299 | |
300 | dev_dbg(&client->dev, "%u connected LEDs\n", | |
301 | rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1); | |
302 | ||
303 | if (rval & AS_FAULT_INFO_TIMEOUT) | |
304 | dev_dbg(&client->dev, "Timeout fault\n"); | |
305 | ||
306 | if (rval & AS_FAULT_INFO_OVER_TEMPERATURE) | |
307 | dev_dbg(&client->dev, "Over temperature fault\n"); | |
308 | ||
309 | if (rval & AS_FAULT_INFO_SHORT_CIRCUIT) | |
310 | dev_dbg(&client->dev, "Short circuit fault\n"); | |
311 | ||
312 | if (rval & AS_FAULT_INFO_OVER_VOLTAGE) | |
4ac29941 AS |
313 | dev_dbg(&client->dev, |
314 | "Over voltage fault: Indicates missing capacitor or open connection\n"); | |
22db44cb LP |
315 | |
316 | return rval; | |
317 | } | |
318 | ||
319 | static int as3645a_get_ctrl(struct v4l2_ctrl *ctrl) | |
320 | { | |
321 | struct as3645a *flash = | |
322 | container_of(ctrl->handler, struct as3645a, ctrls); | |
323 | struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); | |
324 | int value; | |
325 | ||
326 | switch (ctrl->id) { | |
327 | case V4L2_CID_FLASH_FAULT: | |
328 | value = as3645a_read_fault(flash); | |
329 | if (value < 0) | |
330 | return value; | |
331 | ||
332 | ctrl->cur.val = 0; | |
333 | if (value & AS_FAULT_INFO_SHORT_CIRCUIT) | |
334 | ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT; | |
335 | if (value & AS_FAULT_INFO_OVER_TEMPERATURE) | |
336 | ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE; | |
337 | if (value & AS_FAULT_INFO_TIMEOUT) | |
338 | ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT; | |
339 | if (value & AS_FAULT_INFO_OVER_VOLTAGE) | |
340 | ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE; | |
341 | if (value & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT) | |
342 | ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_CURRENT; | |
343 | if (value & AS_FAULT_INFO_INDICATOR_LED) | |
344 | ctrl->cur.val |= V4L2_FLASH_FAULT_INDICATOR; | |
345 | break; | |
346 | ||
347 | case V4L2_CID_FLASH_STROBE_STATUS: | |
348 | if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) { | |
349 | ctrl->cur.val = 0; | |
350 | break; | |
351 | } | |
352 | ||
353 | value = as3645a_is_active(flash); | |
354 | if (value < 0) | |
355 | return value; | |
356 | ||
357 | ctrl->cur.val = value; | |
358 | break; | |
359 | } | |
360 | ||
361 | dev_dbg(&client->dev, "G_CTRL %08x:%d\n", ctrl->id, ctrl->cur.val); | |
362 | ||
363 | return 0; | |
364 | } | |
365 | ||
366 | static int as3645a_set_ctrl(struct v4l2_ctrl *ctrl) | |
367 | { | |
368 | struct as3645a *flash = | |
369 | container_of(ctrl->handler, struct as3645a, ctrls); | |
370 | struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); | |
371 | int ret; | |
372 | ||
373 | dev_dbg(&client->dev, "S_CTRL %08x:%d\n", ctrl->id, ctrl->val); | |
374 | ||
375 | /* If a control that doesn't apply to the current mode is modified, | |
376 | * we store the value and return immediately. The setting will be | |
377 | * applied when the LED mode is changed. Otherwise we apply the setting | |
378 | * immediately. | |
379 | */ | |
380 | ||
381 | switch (ctrl->id) { | |
382 | case V4L2_CID_FLASH_LED_MODE: | |
383 | if (flash->indicator_current) | |
384 | return -EBUSY; | |
385 | ||
386 | ret = as3645a_set_config(flash); | |
387 | if (ret < 0) | |
388 | return ret; | |
389 | ||
390 | flash->led_mode = ctrl->val; | |
391 | return as3645a_set_output(flash, false); | |
392 | ||
393 | case V4L2_CID_FLASH_STROBE_SOURCE: | |
394 | flash->strobe_source = ctrl->val; | |
395 | ||
396 | /* Applies to flash mode only. */ | |
397 | if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) | |
398 | break; | |
399 | ||
400 | return as3645a_set_output(flash, false); | |
401 | ||
402 | case V4L2_CID_FLASH_STROBE: | |
403 | if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) | |
404 | return -EBUSY; | |
405 | ||
406 | return as3645a_set_output(flash, true); | |
407 | ||
408 | case V4L2_CID_FLASH_STROBE_STOP: | |
409 | if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) | |
410 | return -EBUSY; | |
411 | ||
412 | return as3645a_set_output(flash, false); | |
413 | ||
414 | case V4L2_CID_FLASH_TIMEOUT: | |
415 | flash->timeout = ctrl->val; | |
416 | ||
417 | /* Applies to flash mode only. */ | |
418 | if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) | |
419 | break; | |
420 | ||
421 | return as3645a_set_config(flash); | |
422 | ||
423 | case V4L2_CID_FLASH_INTENSITY: | |
424 | flash->flash_current = (ctrl->val - AS3645A_FLASH_INTENSITY_MIN) | |
425 | / AS3645A_FLASH_INTENSITY_STEP; | |
426 | ||
427 | /* Applies to flash mode only. */ | |
428 | if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) | |
429 | break; | |
430 | ||
431 | return as3645a_set_config(flash); | |
432 | ||
433 | case V4L2_CID_FLASH_TORCH_INTENSITY: | |
434 | flash->assist_current = | |
435 | (ctrl->val - AS3645A_TORCH_INTENSITY_MIN) | |
436 | / AS3645A_TORCH_INTENSITY_STEP; | |
437 | ||
438 | /* Applies to torch mode only. */ | |
439 | if (flash->led_mode != V4L2_FLASH_LED_MODE_TORCH) | |
440 | break; | |
441 | ||
442 | return as3645a_set_config(flash); | |
443 | ||
444 | case V4L2_CID_FLASH_INDICATOR_INTENSITY: | |
445 | if (flash->led_mode != V4L2_FLASH_LED_MODE_NONE) | |
446 | return -EBUSY; | |
447 | ||
448 | flash->indicator_current = | |
449 | (ctrl->val - AS3645A_INDICATOR_INTENSITY_MIN) | |
450 | / AS3645A_INDICATOR_INTENSITY_STEP; | |
451 | ||
452 | ret = as3645a_set_config(flash); | |
453 | if (ret < 0) | |
454 | return ret; | |
455 | ||
456 | if ((ctrl->val == 0) == (ctrl->cur.val == 0)) | |
457 | break; | |
458 | ||
459 | return as3645a_set_output(flash, false); | |
460 | } | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | static const struct v4l2_ctrl_ops as3645a_ctrl_ops = { | |
466 | .g_volatile_ctrl = as3645a_get_ctrl, | |
467 | .s_ctrl = as3645a_set_ctrl, | |
468 | }; | |
469 | ||
470 | /* ----------------------------------------------------------------------------- | |
471 | * V4L2 subdev core operations | |
472 | */ | |
473 | ||
474 | /* Put device into know state. */ | |
475 | static int as3645a_setup(struct as3645a *flash) | |
476 | { | |
477 | struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); | |
478 | int ret; | |
479 | ||
480 | /* clear errors */ | |
481 | ret = as3645a_read(flash, AS_FAULT_INFO_REG); | |
482 | if (ret < 0) | |
483 | return ret; | |
484 | ||
485 | dev_dbg(&client->dev, "Fault info: %02x\n", ret); | |
486 | ||
487 | ret = as3645a_set_config(flash); | |
488 | if (ret < 0) | |
489 | return ret; | |
490 | ||
491 | ret = as3645a_set_output(flash, false); | |
492 | if (ret < 0) | |
493 | return ret; | |
494 | ||
495 | /* read status */ | |
496 | ret = as3645a_read_fault(flash); | |
497 | if (ret < 0) | |
498 | return ret; | |
499 | ||
500 | dev_dbg(&client->dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n", | |
501 | as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG)); | |
502 | dev_dbg(&client->dev, "AS_CURRENT_SET_REG: %02x\n", | |
503 | as3645a_read(flash, AS_CURRENT_SET_REG)); | |
504 | dev_dbg(&client->dev, "AS_CONTROL_REG: %02x\n", | |
505 | as3645a_read(flash, AS_CONTROL_REG)); | |
506 | ||
507 | return ret & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0; | |
508 | } | |
509 | ||
510 | static int __as3645a_set_power(struct as3645a *flash, int on) | |
511 | { | |
512 | int ret; | |
513 | ||
514 | if (!on) | |
515 | as3645a_set_control(flash, AS_MODE_EXT_TORCH, false); | |
516 | ||
517 | if (flash->pdata->set_power) { | |
518 | ret = flash->pdata->set_power(&flash->subdev, on); | |
519 | if (ret < 0) | |
520 | return ret; | |
521 | } | |
522 | ||
523 | if (!on) | |
524 | return 0; | |
525 | ||
526 | ret = as3645a_setup(flash); | |
527 | if (ret < 0) { | |
528 | if (flash->pdata->set_power) | |
529 | flash->pdata->set_power(&flash->subdev, 0); | |
530 | } | |
531 | ||
532 | return ret; | |
533 | } | |
534 | ||
535 | static int as3645a_set_power(struct v4l2_subdev *sd, int on) | |
536 | { | |
537 | struct as3645a *flash = to_as3645a(sd); | |
538 | int ret = 0; | |
539 | ||
540 | mutex_lock(&flash->power_lock); | |
541 | ||
542 | if (flash->power_count == !on) { | |
543 | ret = __as3645a_set_power(flash, !!on); | |
544 | if (ret < 0) | |
545 | goto done; | |
546 | } | |
547 | ||
548 | flash->power_count += on ? 1 : -1; | |
549 | WARN_ON(flash->power_count < 0); | |
550 | ||
551 | done: | |
552 | mutex_unlock(&flash->power_lock); | |
553 | return ret; | |
554 | } | |
555 | ||
556 | static int as3645a_registered(struct v4l2_subdev *sd) | |
557 | { | |
558 | struct as3645a *flash = to_as3645a(sd); | |
559 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
560 | int rval, man, model, rfu, version; | |
561 | const char *vendor; | |
562 | ||
563 | /* Power up the flash driver and read manufacturer ID, model ID, RFU | |
564 | * and version. | |
565 | */ | |
566 | rval = as3645a_set_power(&flash->subdev, 1); | |
567 | if (rval < 0) | |
568 | return rval; | |
569 | ||
570 | rval = as3645a_read(flash, AS_DESIGN_INFO_REG); | |
571 | if (rval < 0) | |
572 | goto power_off; | |
573 | ||
574 | man = AS_DESIGN_INFO_FACTORY(rval); | |
575 | model = AS_DESIGN_INFO_MODEL(rval); | |
576 | ||
577 | rval = as3645a_read(flash, AS_VERSION_CONTROL_REG); | |
578 | if (rval < 0) | |
579 | goto power_off; | |
580 | ||
581 | rfu = AS_VERSION_CONTROL_RFU(rval); | |
582 | version = AS_VERSION_CONTROL_VERSION(rval); | |
583 | ||
584 | /* Verify the chip model and version. */ | |
585 | if (model != 0x01 || rfu != 0x00) { | |
4ac29941 AS |
586 | dev_err(&client->dev, |
587 | "AS3645A not detected (model %d rfu %d)\n", model, rfu); | |
22db44cb LP |
588 | rval = -ENODEV; |
589 | goto power_off; | |
590 | } | |
591 | ||
592 | switch (man) { | |
593 | case 1: | |
594 | vendor = "AMS, Austria Micro Systems"; | |
595 | break; | |
596 | case 2: | |
597 | vendor = "ADI, Analog Devices Inc."; | |
598 | break; | |
599 | case 3: | |
600 | vendor = "NSC, National Semiconductor"; | |
601 | break; | |
602 | case 4: | |
603 | vendor = "NXP"; | |
604 | break; | |
605 | case 5: | |
606 | vendor = "TI, Texas Instrument"; | |
607 | break; | |
608 | default: | |
609 | vendor = "Unknown"; | |
610 | } | |
611 | ||
612 | dev_info(&client->dev, "Chip vendor: %s (%d) Version: %d\n", vendor, | |
613 | man, version); | |
614 | ||
615 | rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE); | |
616 | if (rval < 0) | |
617 | goto power_off; | |
618 | ||
619 | rval = as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE); | |
620 | if (rval < 0) | |
621 | goto power_off; | |
622 | ||
623 | /* Setup default values. This makes sure that the chip is in a known | |
624 | * state, in case the power rail can't be controlled. | |
625 | */ | |
626 | rval = as3645a_setup(flash); | |
627 | ||
628 | power_off: | |
629 | as3645a_set_power(&flash->subdev, 0); | |
630 | ||
631 | return rval; | |
632 | } | |
633 | ||
634 | static int as3645a_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) | |
635 | { | |
636 | return as3645a_set_power(sd, 1); | |
637 | } | |
638 | ||
639 | static int as3645a_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) | |
640 | { | |
641 | return as3645a_set_power(sd, 0); | |
642 | } | |
643 | ||
644 | static const struct v4l2_subdev_core_ops as3645a_core_ops = { | |
645 | .s_power = as3645a_set_power, | |
646 | }; | |
647 | ||
648 | static const struct v4l2_subdev_ops as3645a_ops = { | |
649 | .core = &as3645a_core_ops, | |
650 | }; | |
651 | ||
652 | static const struct v4l2_subdev_internal_ops as3645a_internal_ops = { | |
653 | .registered = as3645a_registered, | |
654 | .open = as3645a_open, | |
655 | .close = as3645a_close, | |
656 | }; | |
657 | ||
658 | /* ----------------------------------------------------------------------------- | |
659 | * I2C driver | |
660 | */ | |
661 | #ifdef CONFIG_PM | |
662 | ||
663 | static int as3645a_suspend(struct device *dev) | |
664 | { | |
665 | struct i2c_client *client = to_i2c_client(dev); | |
666 | struct v4l2_subdev *subdev = i2c_get_clientdata(client); | |
667 | struct as3645a *flash = to_as3645a(subdev); | |
668 | int rval; | |
669 | ||
670 | if (flash->power_count == 0) | |
671 | return 0; | |
672 | ||
673 | rval = __as3645a_set_power(flash, 0); | |
674 | ||
675 | dev_dbg(&client->dev, "Suspend %s\n", rval < 0 ? "failed" : "ok"); | |
676 | ||
677 | return rval; | |
678 | } | |
679 | ||
680 | static int as3645a_resume(struct device *dev) | |
681 | { | |
682 | struct i2c_client *client = to_i2c_client(dev); | |
683 | struct v4l2_subdev *subdev = i2c_get_clientdata(client); | |
684 | struct as3645a *flash = to_as3645a(subdev); | |
685 | int rval; | |
686 | ||
687 | if (flash->power_count == 0) | |
688 | return 0; | |
689 | ||
690 | rval = __as3645a_set_power(flash, 1); | |
691 | ||
692 | dev_dbg(&client->dev, "Resume %s\n", rval < 0 ? "fail" : "ok"); | |
693 | ||
694 | return rval; | |
695 | } | |
696 | ||
697 | #else | |
698 | ||
699 | #define as3645a_suspend NULL | |
700 | #define as3645a_resume NULL | |
701 | ||
702 | #endif /* CONFIG_PM */ | |
703 | ||
704 | /* | |
705 | * as3645a_init_controls - Create controls | |
706 | * @flash: The flash | |
707 | * | |
708 | * The number of LEDs reported in platform data is used to compute default | |
709 | * limits. Parameters passed through platform data can override those limits. | |
710 | */ | |
4c62e976 | 711 | static int as3645a_init_controls(struct as3645a *flash) |
22db44cb LP |
712 | { |
713 | const struct as3645a_platform_data *pdata = flash->pdata; | |
714 | struct v4l2_ctrl *ctrl; | |
715 | int maximum; | |
716 | ||
717 | v4l2_ctrl_handler_init(&flash->ctrls, 10); | |
718 | ||
719 | /* V4L2_CID_FLASH_LED_MODE */ | |
720 | v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops, | |
721 | V4L2_CID_FLASH_LED_MODE, 2, ~7, | |
722 | V4L2_FLASH_LED_MODE_NONE); | |
723 | ||
724 | /* V4L2_CID_FLASH_STROBE_SOURCE */ | |
725 | v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops, | |
726 | V4L2_CID_FLASH_STROBE_SOURCE, | |
727 | pdata->ext_strobe ? 1 : 0, | |
728 | pdata->ext_strobe ? ~3 : ~1, | |
729 | V4L2_FLASH_STROBE_SOURCE_SOFTWARE); | |
730 | ||
731 | flash->strobe_source = V4L2_FLASH_STROBE_SOURCE_SOFTWARE; | |
732 | ||
733 | /* V4L2_CID_FLASH_STROBE */ | |
734 | v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops, | |
735 | V4L2_CID_FLASH_STROBE, 0, 0, 0, 0); | |
736 | ||
737 | /* V4L2_CID_FLASH_STROBE_STOP */ | |
738 | v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops, | |
739 | V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0); | |
740 | ||
741 | /* V4L2_CID_FLASH_STROBE_STATUS */ | |
742 | ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops, | |
743 | V4L2_CID_FLASH_STROBE_STATUS, 0, 1, 1, 1); | |
744 | if (ctrl != NULL) | |
745 | ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; | |
746 | ||
747 | /* V4L2_CID_FLASH_TIMEOUT */ | |
748 | maximum = pdata->timeout_max; | |
749 | ||
750 | v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops, | |
751 | V4L2_CID_FLASH_TIMEOUT, AS3645A_FLASH_TIMEOUT_MIN, | |
752 | maximum, AS3645A_FLASH_TIMEOUT_STEP, maximum); | |
753 | ||
754 | flash->timeout = maximum; | |
755 | ||
756 | /* V4L2_CID_FLASH_INTENSITY */ | |
757 | maximum = pdata->flash_max_current; | |
758 | ||
759 | v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops, | |
760 | V4L2_CID_FLASH_INTENSITY, AS3645A_FLASH_INTENSITY_MIN, | |
761 | maximum, AS3645A_FLASH_INTENSITY_STEP, maximum); | |
762 | ||
763 | flash->flash_current = (maximum - AS3645A_FLASH_INTENSITY_MIN) | |
764 | / AS3645A_FLASH_INTENSITY_STEP; | |
765 | ||
766 | /* V4L2_CID_FLASH_TORCH_INTENSITY */ | |
767 | maximum = pdata->torch_max_current; | |
768 | ||
769 | v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops, | |
770 | V4L2_CID_FLASH_TORCH_INTENSITY, | |
771 | AS3645A_TORCH_INTENSITY_MIN, maximum, | |
772 | AS3645A_TORCH_INTENSITY_STEP, | |
773 | AS3645A_TORCH_INTENSITY_MIN); | |
774 | ||
775 | flash->assist_current = 0; | |
776 | ||
777 | /* V4L2_CID_FLASH_INDICATOR_INTENSITY */ | |
778 | v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops, | |
779 | V4L2_CID_FLASH_INDICATOR_INTENSITY, | |
780 | AS3645A_INDICATOR_INTENSITY_MIN, | |
781 | AS3645A_INDICATOR_INTENSITY_MAX, | |
782 | AS3645A_INDICATOR_INTENSITY_STEP, | |
783 | AS3645A_INDICATOR_INTENSITY_MIN); | |
784 | ||
785 | flash->indicator_current = 0; | |
786 | ||
787 | /* V4L2_CID_FLASH_FAULT */ | |
788 | ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops, | |
789 | V4L2_CID_FLASH_FAULT, 0, | |
790 | V4L2_FLASH_FAULT_OVER_VOLTAGE | | |
791 | V4L2_FLASH_FAULT_TIMEOUT | | |
792 | V4L2_FLASH_FAULT_OVER_TEMPERATURE | | |
793 | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0); | |
794 | if (ctrl != NULL) | |
795 | ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; | |
796 | ||
797 | flash->subdev.ctrl_handler = &flash->ctrls; | |
798 | ||
799 | return flash->ctrls.error; | |
800 | } | |
801 | ||
4c62e976 GKH |
802 | static int as3645a_probe(struct i2c_client *client, |
803 | const struct i2c_device_id *devid) | |
22db44cb LP |
804 | { |
805 | struct as3645a *flash; | |
806 | int ret; | |
807 | ||
808 | if (client->dev.platform_data == NULL) | |
809 | return -ENODEV; | |
810 | ||
c02b211d | 811 | flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL); |
22db44cb LP |
812 | if (flash == NULL) |
813 | return -ENOMEM; | |
814 | ||
815 | flash->pdata = client->dev.platform_data; | |
816 | ||
817 | v4l2_i2c_subdev_init(&flash->subdev, client, &as3645a_ops); | |
818 | flash->subdev.internal_ops = &as3645a_internal_ops; | |
819 | flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; | |
820 | ||
821 | ret = as3645a_init_controls(flash); | |
822 | if (ret < 0) | |
823 | goto done; | |
824 | ||
ab22e77c | 825 | ret = media_entity_pads_init(&flash->subdev.entity, 0, NULL); |
22db44cb LP |
826 | if (ret < 0) |
827 | goto done; | |
828 | ||
4ca72efa | 829 | flash->subdev.entity.function = MEDIA_ENT_F_FLASH; |
22db44cb LP |
830 | |
831 | mutex_init(&flash->power_lock); | |
832 | ||
833 | flash->led_mode = V4L2_FLASH_LED_MODE_NONE; | |
834 | ||
835 | done: | |
c02b211d | 836 | if (ret < 0) |
22db44cb | 837 | v4l2_ctrl_handler_free(&flash->ctrls); |
22db44cb LP |
838 | |
839 | return ret; | |
840 | } | |
841 | ||
4c62e976 | 842 | static int as3645a_remove(struct i2c_client *client) |
22db44cb LP |
843 | { |
844 | struct v4l2_subdev *subdev = i2c_get_clientdata(client); | |
845 | struct as3645a *flash = to_as3645a(subdev); | |
846 | ||
847 | v4l2_device_unregister_subdev(subdev); | |
848 | v4l2_ctrl_handler_free(&flash->ctrls); | |
849 | media_entity_cleanup(&flash->subdev.entity); | |
850 | mutex_destroy(&flash->power_lock); | |
22db44cb LP |
851 | |
852 | return 0; | |
853 | } | |
854 | ||
855 | static const struct i2c_device_id as3645a_id_table[] = { | |
856 | { AS3645A_NAME, 0 }, | |
857 | { }, | |
858 | }; | |
859 | MODULE_DEVICE_TABLE(i2c, as3645a_id_table); | |
860 | ||
861 | static const struct dev_pm_ops as3645a_pm_ops = { | |
862 | .suspend = as3645a_suspend, | |
863 | .resume = as3645a_resume, | |
864 | }; | |
865 | ||
866 | static struct i2c_driver as3645a_i2c_driver = { | |
867 | .driver = { | |
868 | .name = AS3645A_NAME, | |
869 | .pm = &as3645a_pm_ops, | |
870 | }, | |
871 | .probe = as3645a_probe, | |
4c62e976 | 872 | .remove = as3645a_remove, |
22db44cb LP |
873 | .id_table = as3645a_id_table, |
874 | }; | |
875 | ||
c6e8d86f | 876 | module_i2c_driver(as3645a_i2c_driver); |
22db44cb LP |
877 | |
878 | MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); | |
879 | MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones"); | |
880 | MODULE_LICENSE("GPL"); |