• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * drivers/leds/leds-as3645a.c - AS3645A and LM3555 flash controllers driver
3  *
4  * Copyright (C) 2008-2011 Nokia Corporation
5  * Copyright (c) 2011, 2017 Intel Corporation.
6  *
7  * Based on drivers/media/i2c/as3645a.c.
8  *
9  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * version 2 as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  */
20 
21 #include <linux/delay.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/i2c.h>
24 #include <linux/led-class-flash.h>
25 #include <linux/leds.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/of.h>
29 #include <linux/slab.h>
30 
31 #include <media/v4l2-flash-led-class.h>
32 
33 #define AS_TIMER_US_TO_CODE(t)			(((t) / 1000 - 100) / 50)
34 #define AS_TIMER_CODE_TO_US(c)			((50 * (c) + 100) * 1000)
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 #define AS_NAME					"as3645a"
92 #define AS_I2C_ADDR				(0x60 >> 1) /* W:0x60, R:0x61 */
93 
94 #define AS_FLASH_TIMEOUT_MIN			100000	/* us */
95 #define AS_FLASH_TIMEOUT_MAX			850000
96 #define AS_FLASH_TIMEOUT_STEP			50000
97 
98 #define AS_FLASH_INTENSITY_MIN			200000	/* uA */
99 #define AS_FLASH_INTENSITY_MAX_1LED		500000
100 #define AS_FLASH_INTENSITY_MAX_2LEDS		400000
101 #define AS_FLASH_INTENSITY_STEP			20000
102 
103 #define AS_TORCH_INTENSITY_MIN			20000	/* uA */
104 #define AS_TORCH_INTENSITY_MAX			160000
105 #define AS_TORCH_INTENSITY_STEP			20000
106 
107 #define AS_INDICATOR_INTENSITY_MIN		0	/* uA */
108 #define AS_INDICATOR_INTENSITY_MAX		10000
109 #define AS_INDICATOR_INTENSITY_STEP		2500
110 
111 #define AS_PEAK_mA_MAX				2000
112 #define AS_PEAK_mA_TO_REG(a) \
113 	((min_t(u32, AS_PEAK_mA_MAX, a) - 1250) / 250)
114 
115 /* LED numbers for Devicetree */
116 #define AS_LED_FLASH				0
117 #define AS_LED_INDICATOR			1
118 
119 enum as_mode {
120 	AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT,
121 	AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT,
122 	AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT,
123 	AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT,
124 };
125 
126 struct as3645a_config {
127 	u32 flash_timeout_us;
128 	u32 flash_max_ua;
129 	u32 assist_max_ua;
130 	u32 indicator_max_ua;
131 	u32 voltage_reference;
132 	u32 peak;
133 };
134 
135 struct as3645a_names {
136 	char flash[32];
137 	char indicator[32];
138 };
139 
140 struct as3645a {
141 	struct i2c_client *client;
142 
143 	struct mutex mutex;
144 
145 	struct led_classdev_flash fled;
146 	struct led_classdev iled_cdev;
147 
148 	struct v4l2_flash *vf;
149 	struct v4l2_flash *vfind;
150 
151 	struct device_node *flash_node;
152 	struct device_node *indicator_node;
153 
154 	struct as3645a_config cfg;
155 
156 	enum as_mode mode;
157 	unsigned int timeout;
158 	unsigned int flash_current;
159 	unsigned int assist_current;
160 	unsigned int indicator_current;
161 	enum v4l2_flash_strobe_source strobe_source;
162 };
163 
164 #define fled_to_as3645a(__fled) container_of(__fled, struct as3645a, fled)
165 #define iled_cdev_to_as3645a(__iled_cdev) \
166 	container_of(__iled_cdev, struct as3645a, iled_cdev)
167 
168 /* Return negative errno else zero on success */
as3645a_write(struct as3645a * flash,u8 addr,u8 val)169 static int as3645a_write(struct as3645a *flash, u8 addr, u8 val)
170 {
171 	struct i2c_client *client = flash->client;
172 	int rval;
173 
174 	rval = i2c_smbus_write_byte_data(client, addr, val);
175 
176 	dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
177 		rval < 0 ? "fail" : "ok");
178 
179 	return rval;
180 }
181 
182 /* Return negative errno else a data byte received from the device. */
as3645a_read(struct as3645a * flash,u8 addr)183 static int as3645a_read(struct as3645a *flash, u8 addr)
184 {
185 	struct i2c_client *client = flash->client;
186 	int rval;
187 
188 	rval = i2c_smbus_read_byte_data(client, addr);
189 
190 	dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval,
191 		rval < 0 ? "fail" : "ok");
192 
193 	return rval;
194 }
195 
196 /* -----------------------------------------------------------------------------
197  * Hardware configuration and trigger
198  */
199 
200 /**
201  * as3645a_set_config - Set flash configuration registers
202  * @flash: The flash
203  *
204  * Configure the hardware with flash, assist and indicator currents, as well as
205  * flash timeout.
206  *
207  * Return 0 on success, or a negative error code if an I2C communication error
208  * occurred.
209  */
as3645a_set_current(struct as3645a * flash)210 static int as3645a_set_current(struct as3645a *flash)
211 {
212 	u8 val;
213 
214 	val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT)
215 	    | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT)
216 	    | AS_CURRENT_LED_DET_ON;
217 
218 	return as3645a_write(flash, AS_CURRENT_SET_REG, val);
219 }
220 
as3645a_set_timeout(struct as3645a * flash)221 static int as3645a_set_timeout(struct as3645a *flash)
222 {
223 	u8 val;
224 
225 	val = flash->timeout << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT;
226 
227 	val |= (flash->cfg.voltage_reference
228 		<< AS_INDICATOR_AND_TIMER_VREF_SHIFT)
229 	    |  ((flash->indicator_current ? flash->indicator_current - 1 : 0)
230 		 << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT);
231 
232 	return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val);
233 }
234 
235 /**
236  * as3645a_set_control - Set flash control register
237  * @flash: The flash
238  * @mode: Desired output mode
239  * @on: Desired output state
240  *
241  * Configure the hardware with output mode and state.
242  *
243  * Return 0 on success, or a negative error code if an I2C communication error
244  * occurred.
245  */
246 static int
as3645a_set_control(struct as3645a * flash,enum as_mode mode,bool on)247 as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on)
248 {
249 	u8 reg;
250 
251 	/* Configure output parameters and operation mode. */
252 	reg = (flash->cfg.peak << AS_CONTROL_COIL_PEAK_SHIFT)
253 	    | (on ? AS_CONTROL_OUT_ON : 0)
254 	    | mode;
255 
256 	if (mode == AS_MODE_FLASH &&
257 	    flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL)
258 		reg |= AS_CONTROL_STROBE_TYPE_LEVEL
259 		    |  AS_CONTROL_STROBE_ON;
260 
261 	return as3645a_write(flash, AS_CONTROL_REG, reg);
262 }
263 
as3645a_get_fault(struct led_classdev_flash * fled,u32 * fault)264 static int as3645a_get_fault(struct led_classdev_flash *fled, u32 *fault)
265 {
266 	struct as3645a *flash = fled_to_as3645a(fled);
267 	int rval;
268 
269 	/* NOTE: reading register clears fault status */
270 	rval = as3645a_read(flash, AS_FAULT_INFO_REG);
271 	if (rval < 0)
272 		return rval;
273 
274 	if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
275 		*fault |= LED_FAULT_OVER_CURRENT;
276 
277 	if (rval & AS_FAULT_INFO_INDICATOR_LED)
278 		*fault |= LED_FAULT_INDICATOR;
279 
280 	dev_dbg(&flash->client->dev, "%u connected LEDs\n",
281 		rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1);
282 
283 	if (rval & AS_FAULT_INFO_TIMEOUT)
284 		*fault |= LED_FAULT_TIMEOUT;
285 
286 	if (rval & AS_FAULT_INFO_OVER_TEMPERATURE)
287 		*fault |= LED_FAULT_OVER_TEMPERATURE;
288 
289 	if (rval & AS_FAULT_INFO_SHORT_CIRCUIT)
290 		*fault |= LED_FAULT_OVER_CURRENT;
291 
292 	if (rval & AS_FAULT_INFO_OVER_VOLTAGE)
293 		*fault |= LED_FAULT_INPUT_VOLTAGE;
294 
295 	return rval;
296 }
297 
__as3645a_current_to_reg(unsigned int min,unsigned int max,unsigned int step,unsigned int val)298 static unsigned int __as3645a_current_to_reg(unsigned int min, unsigned int max,
299 					     unsigned int step,
300 					     unsigned int val)
301 {
302 	if (val < min)
303 		val = min;
304 
305 	if (val > max)
306 		val = max;
307 
308 	return (val - min) / step;
309 }
310 
as3645a_current_to_reg(struct as3645a * flash,bool is_flash,unsigned int ua)311 static unsigned int as3645a_current_to_reg(struct as3645a *flash, bool is_flash,
312 					   unsigned int ua)
313 {
314 	if (is_flash)
315 		return __as3645a_current_to_reg(AS_TORCH_INTENSITY_MIN,
316 						flash->cfg.assist_max_ua,
317 						AS_TORCH_INTENSITY_STEP, ua);
318 	else
319 		return __as3645a_current_to_reg(AS_FLASH_INTENSITY_MIN,
320 						flash->cfg.flash_max_ua,
321 						AS_FLASH_INTENSITY_STEP, ua);
322 }
323 
as3645a_set_indicator_brightness(struct led_classdev * iled_cdev,enum led_brightness brightness)324 static int as3645a_set_indicator_brightness(struct led_classdev *iled_cdev,
325 					    enum led_brightness brightness)
326 {
327 	struct as3645a *flash = iled_cdev_to_as3645a(iled_cdev);
328 	int rval;
329 
330 	flash->indicator_current = brightness;
331 
332 	rval = as3645a_set_timeout(flash);
333 	if (rval)
334 		return rval;
335 
336 	return as3645a_set_control(flash, AS_MODE_INDICATOR, brightness);
337 }
338 
as3645a_set_assist_brightness(struct led_classdev * fled_cdev,enum led_brightness brightness)339 static int as3645a_set_assist_brightness(struct led_classdev *fled_cdev,
340 					 enum led_brightness brightness)
341 {
342 	struct led_classdev_flash *fled = lcdev_to_flcdev(fled_cdev);
343 	struct as3645a *flash = fled_to_as3645a(fled);
344 	int rval;
345 
346 	if (brightness) {
347 		/* Register value 0 is 20 mA. */
348 		flash->assist_current = brightness - 1;
349 
350 		rval = as3645a_set_current(flash);
351 		if (rval)
352 			return rval;
353 	}
354 
355 	return as3645a_set_control(flash, AS_MODE_ASSIST, brightness);
356 }
357 
as3645a_set_flash_brightness(struct led_classdev_flash * fled,u32 brightness_ua)358 static int as3645a_set_flash_brightness(struct led_classdev_flash *fled,
359 					u32 brightness_ua)
360 {
361 	struct as3645a *flash = fled_to_as3645a(fled);
362 
363 	flash->flash_current = as3645a_current_to_reg(flash, true, brightness_ua);
364 
365 	return as3645a_set_current(flash);
366 }
367 
as3645a_set_flash_timeout(struct led_classdev_flash * fled,u32 timeout_us)368 static int as3645a_set_flash_timeout(struct led_classdev_flash *fled,
369 				     u32 timeout_us)
370 {
371 	struct as3645a *flash = fled_to_as3645a(fled);
372 
373 	flash->timeout = AS_TIMER_US_TO_CODE(timeout_us);
374 
375 	return as3645a_set_timeout(flash);
376 }
377 
as3645a_set_strobe(struct led_classdev_flash * fled,bool state)378 static int as3645a_set_strobe(struct led_classdev_flash *fled, bool state)
379 {
380 	struct as3645a *flash = fled_to_as3645a(fled);
381 
382 	return as3645a_set_control(flash, AS_MODE_FLASH, state);
383 }
384 
385 static const struct led_flash_ops as3645a_led_flash_ops = {
386 	.flash_brightness_set = as3645a_set_flash_brightness,
387 	.timeout_set = as3645a_set_flash_timeout,
388 	.strobe_set = as3645a_set_strobe,
389 	.fault_get = as3645a_get_fault,
390 };
391 
as3645a_setup(struct as3645a * flash)392 static int as3645a_setup(struct as3645a *flash)
393 {
394 	struct device *dev = &flash->client->dev;
395 	u32 fault = 0;
396 	int rval;
397 
398 	/* clear errors */
399 	rval = as3645a_read(flash, AS_FAULT_INFO_REG);
400 	if (rval < 0)
401 		return rval;
402 
403 	dev_dbg(dev, "Fault info: %02x\n", rval);
404 
405 	rval = as3645a_set_current(flash);
406 	if (rval < 0)
407 		return rval;
408 
409 	rval = as3645a_set_timeout(flash);
410 	if (rval < 0)
411 		return rval;
412 
413 	rval = as3645a_set_control(flash, AS_MODE_INDICATOR, false);
414 	if (rval < 0)
415 		return rval;
416 
417 	/* read status */
418 	rval = as3645a_get_fault(&flash->fled, &fault);
419 	if (rval < 0)
420 		return rval;
421 
422 	dev_dbg(dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n",
423 		as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG));
424 	dev_dbg(dev, "AS_CURRENT_SET_REG: %02x\n",
425 		as3645a_read(flash, AS_CURRENT_SET_REG));
426 	dev_dbg(dev, "AS_CONTROL_REG: %02x\n",
427 		as3645a_read(flash, AS_CONTROL_REG));
428 
429 	return rval & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
430 }
431 
as3645a_detect(struct as3645a * flash)432 static int as3645a_detect(struct as3645a *flash)
433 {
434 	struct device *dev = &flash->client->dev;
435 	int rval, man, model, rfu, version;
436 	const char *vendor;
437 
438 	rval = as3645a_read(flash, AS_DESIGN_INFO_REG);
439 	if (rval < 0) {
440 		dev_err(dev, "can't read design info reg\n");
441 		return rval;
442 	}
443 
444 	man = AS_DESIGN_INFO_FACTORY(rval);
445 	model = AS_DESIGN_INFO_MODEL(rval);
446 
447 	rval = as3645a_read(flash, AS_VERSION_CONTROL_REG);
448 	if (rval < 0) {
449 		dev_err(dev, "can't read version control reg\n");
450 		return rval;
451 	}
452 
453 	rfu = AS_VERSION_CONTROL_RFU(rval);
454 	version = AS_VERSION_CONTROL_VERSION(rval);
455 
456 	/* Verify the chip model and version. */
457 	if (model != 0x01 || rfu != 0x00) {
458 		dev_err(dev, "AS3645A not detected "
459 			"(model %d rfu %d)\n", model, rfu);
460 		return -ENODEV;
461 	}
462 
463 	switch (man) {
464 	case 1:
465 		vendor = "AMS, Austria Micro Systems";
466 		break;
467 	case 2:
468 		vendor = "ADI, Analog Devices Inc.";
469 		break;
470 	case 3:
471 		vendor = "NSC, National Semiconductor";
472 		break;
473 	case 4:
474 		vendor = "NXP";
475 		break;
476 	case 5:
477 		vendor = "TI, Texas Instrument";
478 		break;
479 	default:
480 		vendor = "Unknown";
481 	}
482 
483 	dev_info(dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
484 		 man, version);
485 
486 	rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
487 	if (rval < 0)
488 		return rval;
489 
490 	return as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
491 }
492 
as3645a_parse_node(struct as3645a * flash,struct as3645a_names * names,struct device_node * node)493 static int as3645a_parse_node(struct as3645a *flash,
494 			      struct as3645a_names *names,
495 			      struct device_node *node)
496 {
497 	struct as3645a_config *cfg = &flash->cfg;
498 	struct device_node *child;
499 	const char *name;
500 	int rval;
501 
502 	for_each_child_of_node(node, child) {
503 		u32 id = 0;
504 
505 		of_property_read_u32(child, "reg", &id);
506 
507 		switch (id) {
508 		case AS_LED_FLASH:
509 			flash->flash_node = of_node_get(child);
510 			break;
511 		case AS_LED_INDICATOR:
512 			flash->indicator_node = of_node_get(child);
513 			break;
514 		default:
515 			dev_warn(&flash->client->dev,
516 				 "unknown LED %u encountered, ignoring\n", id);
517 			break;
518 		}
519 	}
520 
521 	if (!flash->flash_node) {
522 		dev_err(&flash->client->dev, "can't find flash node\n");
523 		return -ENODEV;
524 	}
525 
526 	rval = of_property_read_string(flash->flash_node, "label", &name);
527 	if (!rval)
528 		strlcpy(names->flash, name, sizeof(names->flash));
529 	else
530 		snprintf(names->flash, sizeof(names->flash),
531 			 "%s:flash", node->name);
532 
533 	rval = of_property_read_u32(flash->flash_node, "flash-timeout-us",
534 				    &cfg->flash_timeout_us);
535 	if (rval < 0) {
536 		dev_err(&flash->client->dev,
537 			"can't read flash-timeout-us property for flash\n");
538 		goto out_err;
539 	}
540 
541 	rval = of_property_read_u32(flash->flash_node, "flash-max-microamp",
542 				    &cfg->flash_max_ua);
543 	if (rval < 0) {
544 		dev_err(&flash->client->dev,
545 			"can't read flash-max-microamp property for flash\n");
546 		goto out_err;
547 	}
548 
549 	rval = of_property_read_u32(flash->flash_node, "led-max-microamp",
550 				    &cfg->assist_max_ua);
551 	if (rval < 0) {
552 		dev_err(&flash->client->dev,
553 			"can't read led-max-microamp property for flash\n");
554 		goto out_err;
555 	}
556 
557 	of_property_read_u32(flash->flash_node, "voltage-reference",
558 			     &cfg->voltage_reference);
559 
560 	of_property_read_u32(flash->flash_node, "ams,input-max-microamp",
561 			     &cfg->peak);
562 	cfg->peak = AS_PEAK_mA_TO_REG(cfg->peak);
563 
564 	if (!flash->indicator_node) {
565 		dev_warn(&flash->client->dev,
566 			 "can't find indicator node\n");
567 		goto out_err;
568 	}
569 
570 	rval = of_property_read_string(flash->indicator_node, "label", &name);
571 	if (!rval)
572 		strlcpy(names->indicator, name, sizeof(names->indicator));
573 	else
574 		snprintf(names->indicator, sizeof(names->indicator),
575 			 "%s:indicator", node->name);
576 
577 	rval = of_property_read_u32(flash->indicator_node, "led-max-microamp",
578 				    &cfg->indicator_max_ua);
579 	if (rval < 0) {
580 		dev_err(&flash->client->dev,
581 			"can't read led-max-microamp property for indicator\n");
582 		goto out_err;
583 	}
584 
585 	return 0;
586 
587 out_err:
588 	of_node_put(flash->flash_node);
589 	of_node_put(flash->indicator_node);
590 
591 	return rval;
592 }
593 
as3645a_led_class_setup(struct as3645a * flash,struct as3645a_names * names)594 static int as3645a_led_class_setup(struct as3645a *flash,
595 				   struct as3645a_names *names)
596 {
597 	struct led_classdev *fled_cdev = &flash->fled.led_cdev;
598 	struct led_classdev *iled_cdev = &flash->iled_cdev;
599 	struct led_flash_setting *cfg;
600 	int rval;
601 
602 	iled_cdev->name = names->indicator;
603 	iled_cdev->brightness_set_blocking = as3645a_set_indicator_brightness;
604 	iled_cdev->max_brightness =
605 		flash->cfg.indicator_max_ua / AS_INDICATOR_INTENSITY_STEP;
606 	iled_cdev->flags = LED_CORE_SUSPENDRESUME;
607 
608 	rval = led_classdev_register(&flash->client->dev, iled_cdev);
609 	if (rval < 0)
610 		return rval;
611 
612 	cfg = &flash->fled.brightness;
613 	cfg->min = AS_FLASH_INTENSITY_MIN;
614 	cfg->max = flash->cfg.flash_max_ua;
615 	cfg->step = AS_FLASH_INTENSITY_STEP;
616 	cfg->val = flash->cfg.flash_max_ua;
617 
618 	cfg = &flash->fled.timeout;
619 	cfg->min = AS_FLASH_TIMEOUT_MIN;
620 	cfg->max = flash->cfg.flash_timeout_us;
621 	cfg->step = AS_FLASH_TIMEOUT_STEP;
622 	cfg->val = flash->cfg.flash_timeout_us;
623 
624 	flash->fled.ops = &as3645a_led_flash_ops;
625 
626 	fled_cdev->name = names->flash;
627 	fled_cdev->brightness_set_blocking = as3645a_set_assist_brightness;
628 	/* Value 0 is off in LED class. */
629 	fled_cdev->max_brightness =
630 		as3645a_current_to_reg(flash, false,
631 				       flash->cfg.assist_max_ua) + 1;
632 	fled_cdev->flags = LED_DEV_CAP_FLASH | LED_CORE_SUSPENDRESUME;
633 
634 	rval = led_classdev_flash_register(&flash->client->dev, &flash->fled);
635 	if (rval) {
636 		led_classdev_unregister(iled_cdev);
637 		dev_err(&flash->client->dev,
638 			"led_classdev_flash_register() failed, error %d\n",
639 			rval);
640 	}
641 
642 	return rval;
643 }
644 
as3645a_v4l2_setup(struct as3645a * flash)645 static int as3645a_v4l2_setup(struct as3645a *flash)
646 {
647 	struct led_classdev_flash *fled = &flash->fled;
648 	struct led_classdev *led = &fled->led_cdev;
649 	struct v4l2_flash_config cfg = {
650 		.intensity = {
651 			.min = AS_TORCH_INTENSITY_MIN,
652 			.max = flash->cfg.assist_max_ua,
653 			.step = AS_TORCH_INTENSITY_STEP,
654 			.val = flash->cfg.assist_max_ua,
655 		},
656 	};
657 	struct v4l2_flash_config cfgind = {
658 		.intensity = {
659 			.min = AS_INDICATOR_INTENSITY_MIN,
660 			.max = flash->cfg.indicator_max_ua,
661 			.step = AS_INDICATOR_INTENSITY_STEP,
662 			.val = flash->cfg.indicator_max_ua,
663 		},
664 	};
665 
666 	strlcpy(cfg.dev_name, led->name, sizeof(cfg.dev_name));
667 	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfg.dev_name));
668 
669 	flash->vf = v4l2_flash_init(
670 		&flash->client->dev, of_fwnode_handle(flash->flash_node),
671 		&flash->fled, NULL, &cfg);
672 	if (IS_ERR(flash->vf))
673 		return PTR_ERR(flash->vf);
674 
675 	flash->vfind = v4l2_flash_indicator_init(
676 		&flash->client->dev, of_fwnode_handle(flash->indicator_node),
677 		&flash->iled_cdev, &cfgind);
678 	if (IS_ERR(flash->vfind)) {
679 		v4l2_flash_release(flash->vf);
680 		return PTR_ERR(flash->vfind);
681 	}
682 
683 	return 0;
684 }
685 
as3645a_probe(struct i2c_client * client)686 static int as3645a_probe(struct i2c_client *client)
687 {
688 	struct as3645a_names names;
689 	struct as3645a *flash;
690 	int rval;
691 
692 	if (client->dev.of_node == NULL)
693 		return -ENODEV;
694 
695 	flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
696 	if (flash == NULL)
697 		return -ENOMEM;
698 
699 	flash->client = client;
700 
701 	rval = as3645a_parse_node(flash, &names, client->dev.of_node);
702 	if (rval < 0)
703 		return rval;
704 
705 	rval = as3645a_detect(flash);
706 	if (rval < 0)
707 		goto out_put_nodes;
708 
709 	mutex_init(&flash->mutex);
710 	i2c_set_clientdata(client, flash);
711 
712 	rval = as3645a_setup(flash);
713 	if (rval)
714 		goto out_mutex_destroy;
715 
716 	rval = as3645a_led_class_setup(flash, &names);
717 	if (rval)
718 		goto out_mutex_destroy;
719 
720 	rval = as3645a_v4l2_setup(flash);
721 	if (rval)
722 		goto out_led_classdev_flash_unregister;
723 
724 	return 0;
725 
726 out_led_classdev_flash_unregister:
727 	led_classdev_flash_unregister(&flash->fled);
728 
729 out_mutex_destroy:
730 	mutex_destroy(&flash->mutex);
731 
732 out_put_nodes:
733 	of_node_put(flash->flash_node);
734 	of_node_put(flash->indicator_node);
735 
736 	return rval;
737 }
738 
as3645a_remove(struct i2c_client * client)739 static int as3645a_remove(struct i2c_client *client)
740 {
741 	struct as3645a *flash = i2c_get_clientdata(client);
742 
743 	as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
744 
745 	v4l2_flash_release(flash->vf);
746 	v4l2_flash_release(flash->vfind);
747 
748 	led_classdev_flash_unregister(&flash->fled);
749 	led_classdev_unregister(&flash->iled_cdev);
750 
751 	mutex_destroy(&flash->mutex);
752 
753 	of_node_put(flash->flash_node);
754 	of_node_put(flash->indicator_node);
755 
756 	return 0;
757 }
758 
759 static const struct i2c_device_id as3645a_id_table[] = {
760 	{ AS_NAME, 0 },
761 	{ },
762 };
763 MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
764 
765 static const struct of_device_id as3645a_of_table[] = {
766 	{ .compatible = "ams,as3645a" },
767 	{ },
768 };
769 MODULE_DEVICE_TABLE(of, as3645a_of_table);
770 
771 static struct i2c_driver as3645a_i2c_driver = {
772 	.driver	= {
773 		.of_match_table = as3645a_of_table,
774 		.name = AS_NAME,
775 	},
776 	.probe_new	= as3645a_probe,
777 	.remove	= as3645a_remove,
778 	.id_table = as3645a_id_table,
779 };
780 
781 module_i2c_driver(as3645a_i2c_driver);
782 
783 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
784 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
785 MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones");
786 MODULE_LICENSE("GPL v2");
787