[go: nahoru, domu]

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