leds-as3645a.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. #include <linux/delay.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/i2c.h>
  23. #include <linux/led-class-flash.h>
  24. #include <linux/leds.h>
  25. #include <linux/module.h>
  26. #include <linux/mutex.h>
  27. #include <linux/of.h>
  28. #include <linux/slab.h>
  29. #include <media/v4l2-flash-led-class.h>
  30. #define AS_TIMER_US_TO_CODE(t) (((t) / 1000 - 100) / 50)
  31. #define AS_TIMER_CODE_TO_US(c) ((50 * (c) + 100) * 1000)
  32. /* Register definitions */
  33. /* Read-only Design info register: Reset state: xxxx 0001 */
  34. #define AS_DESIGN_INFO_REG 0x00
  35. #define AS_DESIGN_INFO_FACTORY(x) (((x) >> 4))
  36. #define AS_DESIGN_INFO_MODEL(x) ((x) & 0x0f)
  37. /* Read-only Version control register: Reset state: 0000 0000
  38. * for first engineering samples
  39. */
  40. #define AS_VERSION_CONTROL_REG 0x01
  41. #define AS_VERSION_CONTROL_RFU(x) (((x) >> 4))
  42. #define AS_VERSION_CONTROL_VERSION(x) ((x) & 0x0f)
  43. /* Read / Write (Indicator and timer register): Reset state: 0000 1111 */
  44. #define AS_INDICATOR_AND_TIMER_REG 0x02
  45. #define AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT 0
  46. #define AS_INDICATOR_AND_TIMER_VREF_SHIFT 4
  47. #define AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT 6
  48. /* Read / Write (Current set register): Reset state: 0110 1001 */
  49. #define AS_CURRENT_SET_REG 0x03
  50. #define AS_CURRENT_ASSIST_LIGHT_SHIFT 0
  51. #define AS_CURRENT_LED_DET_ON (1 << 3)
  52. #define AS_CURRENT_FLASH_CURRENT_SHIFT 4
  53. /* Read / Write (Control register): Reset state: 1011 0100 */
  54. #define AS_CONTROL_REG 0x04
  55. #define AS_CONTROL_MODE_SETTING_SHIFT 0
  56. #define AS_CONTROL_STROBE_ON (1 << 2)
  57. #define AS_CONTROL_OUT_ON (1 << 3)
  58. #define AS_CONTROL_EXT_TORCH_ON (1 << 4)
  59. #define AS_CONTROL_STROBE_TYPE_EDGE (0 << 5)
  60. #define AS_CONTROL_STROBE_TYPE_LEVEL (1 << 5)
  61. #define AS_CONTROL_COIL_PEAK_SHIFT 6
  62. /* Read only (D3 is read / write) (Fault and info): Reset state: 0000 x000 */
  63. #define AS_FAULT_INFO_REG 0x05
  64. #define AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT (1 << 1)
  65. #define AS_FAULT_INFO_INDICATOR_LED (1 << 2)
  66. #define AS_FAULT_INFO_LED_AMOUNT (1 << 3)
  67. #define AS_FAULT_INFO_TIMEOUT (1 << 4)
  68. #define AS_FAULT_INFO_OVER_TEMPERATURE (1 << 5)
  69. #define AS_FAULT_INFO_SHORT_CIRCUIT (1 << 6)
  70. #define AS_FAULT_INFO_OVER_VOLTAGE (1 << 7)
  71. /* Boost register */
  72. #define AS_BOOST_REG 0x0d
  73. #define AS_BOOST_CURRENT_DISABLE (0 << 0)
  74. #define AS_BOOST_CURRENT_ENABLE (1 << 0)
  75. /* Password register is used to unlock boost register writing */
  76. #define AS_PASSWORD_REG 0x0f
  77. #define AS_PASSWORD_UNLOCK_VALUE 0x55
  78. #define AS_NAME "as3645a"
  79. #define AS_I2C_ADDR (0x60 >> 1) /* W:0x60, R:0x61 */
  80. #define AS_FLASH_TIMEOUT_MIN 100000 /* us */
  81. #define AS_FLASH_TIMEOUT_MAX 850000
  82. #define AS_FLASH_TIMEOUT_STEP 50000
  83. #define AS_FLASH_INTENSITY_MIN 200000 /* uA */
  84. #define AS_FLASH_INTENSITY_MAX_1LED 500000
  85. #define AS_FLASH_INTENSITY_MAX_2LEDS 400000
  86. #define AS_FLASH_INTENSITY_STEP 20000
  87. #define AS_TORCH_INTENSITY_MIN 20000 /* uA */
  88. #define AS_TORCH_INTENSITY_MAX 160000
  89. #define AS_TORCH_INTENSITY_STEP 20000
  90. #define AS_INDICATOR_INTENSITY_MIN 0 /* uA */
  91. #define AS_INDICATOR_INTENSITY_MAX 10000
  92. #define AS_INDICATOR_INTENSITY_STEP 2500
  93. #define AS_PEAK_mA_MAX 2000
  94. #define AS_PEAK_mA_TO_REG(a) \
  95. ((min_t(u32, AS_PEAK_mA_MAX, a) - 1250) / 250)
  96. /* LED numbers for Devicetree */
  97. #define AS_LED_FLASH 0
  98. #define AS_LED_INDICATOR 1
  99. enum as_mode {
  100. AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT,
  101. AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT,
  102. AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT,
  103. AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT,
  104. };
  105. struct as3645a_config {
  106. u32 flash_timeout_us;
  107. u32 flash_max_ua;
  108. u32 assist_max_ua;
  109. u32 indicator_max_ua;
  110. u32 voltage_reference;
  111. u32 peak;
  112. };
  113. struct as3645a_names {
  114. char flash[32];
  115. char indicator[32];
  116. };
  117. struct as3645a {
  118. struct i2c_client *client;
  119. struct mutex mutex;
  120. struct led_classdev_flash fled;
  121. struct led_classdev iled_cdev;
  122. struct v4l2_flash *vf;
  123. struct v4l2_flash *vfind;
  124. struct device_node *flash_node;
  125. struct device_node *indicator_node;
  126. struct as3645a_config cfg;
  127. enum as_mode mode;
  128. unsigned int timeout;
  129. unsigned int flash_current;
  130. unsigned int assist_current;
  131. unsigned int indicator_current;
  132. enum v4l2_flash_strobe_source strobe_source;
  133. };
  134. #define fled_to_as3645a(__fled) container_of(__fled, struct as3645a, fled)
  135. #define iled_cdev_to_as3645a(__iled_cdev) \
  136. container_of(__iled_cdev, struct as3645a, iled_cdev)
  137. /* Return negative errno else zero on success */
  138. static int as3645a_write(struct as3645a *flash, u8 addr, u8 val)
  139. {
  140. struct i2c_client *client = flash->client;
  141. int rval;
  142. rval = i2c_smbus_write_byte_data(client, addr, val);
  143. dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
  144. rval < 0 ? "fail" : "ok");
  145. return rval;
  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 = flash->client;
  151. int rval;
  152. rval = i2c_smbus_read_byte_data(client, addr);
  153. dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval,
  154. rval < 0 ? "fail" : "ok");
  155. return rval;
  156. }
  157. /* -----------------------------------------------------------------------------
  158. * Hardware configuration and trigger
  159. */
  160. /**
  161. * as3645a_set_config - Set flash configuration registers
  162. * @flash: The flash
  163. *
  164. * Configure the hardware with flash, assist and indicator currents, as well as
  165. * flash timeout.
  166. *
  167. * Return 0 on success, or a negative error code if an I2C communication error
  168. * occurred.
  169. */
  170. static int as3645a_set_current(struct as3645a *flash)
  171. {
  172. u8 val;
  173. val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT)
  174. | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT)
  175. | AS_CURRENT_LED_DET_ON;
  176. return as3645a_write(flash, AS_CURRENT_SET_REG, val);
  177. }
  178. static int as3645a_set_timeout(struct as3645a *flash)
  179. {
  180. u8 val;
  181. val = flash->timeout << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT;
  182. val |= (flash->cfg.voltage_reference
  183. << AS_INDICATOR_AND_TIMER_VREF_SHIFT)
  184. | ((flash->indicator_current ? flash->indicator_current - 1 : 0)
  185. << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT);
  186. return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val);
  187. }
  188. /**
  189. * as3645a_set_control - Set flash control register
  190. * @flash: The flash
  191. * @mode: Desired output mode
  192. * @on: Desired output state
  193. *
  194. * Configure the hardware with output mode and state.
  195. *
  196. * Return 0 on success, or a negative error code if an I2C communication error
  197. * occurred.
  198. */
  199. static int
  200. as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on)
  201. {
  202. u8 reg;
  203. /* Configure output parameters and operation mode. */
  204. reg = (flash->cfg.peak << AS_CONTROL_COIL_PEAK_SHIFT)
  205. | (on ? AS_CONTROL_OUT_ON : 0)
  206. | mode;
  207. if (mode == AS_MODE_FLASH &&
  208. flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL)
  209. reg |= AS_CONTROL_STROBE_TYPE_LEVEL
  210. | AS_CONTROL_STROBE_ON;
  211. return as3645a_write(flash, AS_CONTROL_REG, reg);
  212. }
  213. static int as3645a_get_fault(struct led_classdev_flash *fled, u32 *fault)
  214. {
  215. struct as3645a *flash = fled_to_as3645a(fled);
  216. int rval;
  217. /* NOTE: reading register clears fault status */
  218. rval = as3645a_read(flash, AS_FAULT_INFO_REG);
  219. if (rval < 0)
  220. return rval;
  221. if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
  222. *fault |= LED_FAULT_OVER_CURRENT;
  223. if (rval & AS_FAULT_INFO_INDICATOR_LED)
  224. *fault |= LED_FAULT_INDICATOR;
  225. dev_dbg(&flash->client->dev, "%u connected LEDs\n",
  226. rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1);
  227. if (rval & AS_FAULT_INFO_TIMEOUT)
  228. *fault |= LED_FAULT_TIMEOUT;
  229. if (rval & AS_FAULT_INFO_OVER_TEMPERATURE)
  230. *fault |= LED_FAULT_OVER_TEMPERATURE;
  231. if (rval & AS_FAULT_INFO_SHORT_CIRCUIT)
  232. *fault |= LED_FAULT_OVER_CURRENT;
  233. if (rval & AS_FAULT_INFO_OVER_VOLTAGE)
  234. *fault |= LED_FAULT_INPUT_VOLTAGE;
  235. return rval;
  236. }
  237. static unsigned int __as3645a_current_to_reg(unsigned int min, unsigned int max,
  238. unsigned int step,
  239. unsigned int val)
  240. {
  241. if (val < min)
  242. val = min;
  243. if (val > max)
  244. val = max;
  245. return (val - min) / step;
  246. }
  247. static unsigned int as3645a_current_to_reg(struct as3645a *flash, bool is_flash,
  248. unsigned int ua)
  249. {
  250. if (is_flash)
  251. return __as3645a_current_to_reg(AS_TORCH_INTENSITY_MIN,
  252. flash->cfg.assist_max_ua,
  253. AS_TORCH_INTENSITY_STEP, ua);
  254. else
  255. return __as3645a_current_to_reg(AS_FLASH_INTENSITY_MIN,
  256. flash->cfg.flash_max_ua,
  257. AS_FLASH_INTENSITY_STEP, ua);
  258. }
  259. static int as3645a_set_indicator_brightness(struct led_classdev *iled_cdev,
  260. enum led_brightness brightness)
  261. {
  262. struct as3645a *flash = iled_cdev_to_as3645a(iled_cdev);
  263. int rval;
  264. flash->indicator_current = brightness;
  265. rval = as3645a_set_timeout(flash);
  266. if (rval)
  267. return rval;
  268. return as3645a_set_control(flash, AS_MODE_INDICATOR, brightness);
  269. }
  270. static int as3645a_set_assist_brightness(struct led_classdev *fled_cdev,
  271. enum led_brightness brightness)
  272. {
  273. struct led_classdev_flash *fled = lcdev_to_flcdev(fled_cdev);
  274. struct as3645a *flash = fled_to_as3645a(fled);
  275. int rval;
  276. if (brightness) {
  277. /* Register value 0 is 20 mA. */
  278. flash->assist_current = brightness - 1;
  279. rval = as3645a_set_current(flash);
  280. if (rval)
  281. return rval;
  282. }
  283. return as3645a_set_control(flash, AS_MODE_ASSIST, brightness);
  284. }
  285. static int as3645a_set_flash_brightness(struct led_classdev_flash *fled,
  286. u32 brightness_ua)
  287. {
  288. struct as3645a *flash = fled_to_as3645a(fled);
  289. flash->flash_current = as3645a_current_to_reg(flash, true,
  290. brightness_ua);
  291. return as3645a_set_current(flash);
  292. }
  293. static int as3645a_set_flash_timeout(struct led_classdev_flash *fled,
  294. u32 timeout_us)
  295. {
  296. struct as3645a *flash = fled_to_as3645a(fled);
  297. flash->timeout = AS_TIMER_US_TO_CODE(timeout_us);
  298. return as3645a_set_timeout(flash);
  299. }
  300. static int as3645a_set_strobe(struct led_classdev_flash *fled, bool state)
  301. {
  302. struct as3645a *flash = fled_to_as3645a(fled);
  303. return as3645a_set_control(flash, AS_MODE_FLASH, state);
  304. }
  305. static const struct led_flash_ops as3645a_led_flash_ops = {
  306. .flash_brightness_set = as3645a_set_flash_brightness,
  307. .timeout_set = as3645a_set_flash_timeout,
  308. .strobe_set = as3645a_set_strobe,
  309. .fault_get = as3645a_get_fault,
  310. };
  311. static int as3645a_setup(struct as3645a *flash)
  312. {
  313. struct device *dev = &flash->client->dev;
  314. u32 fault = 0;
  315. int rval;
  316. /* clear errors */
  317. rval = as3645a_read(flash, AS_FAULT_INFO_REG);
  318. if (rval < 0)
  319. return rval;
  320. dev_dbg(dev, "Fault info: %02x\n", rval);
  321. rval = as3645a_set_current(flash);
  322. if (rval < 0)
  323. return rval;
  324. rval = as3645a_set_timeout(flash);
  325. if (rval < 0)
  326. return rval;
  327. rval = as3645a_set_control(flash, AS_MODE_INDICATOR, false);
  328. if (rval < 0)
  329. return rval;
  330. /* read status */
  331. rval = as3645a_get_fault(&flash->fled, &fault);
  332. if (rval < 0)
  333. return rval;
  334. dev_dbg(dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n",
  335. as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG));
  336. dev_dbg(dev, "AS_CURRENT_SET_REG: %02x\n",
  337. as3645a_read(flash, AS_CURRENT_SET_REG));
  338. dev_dbg(dev, "AS_CONTROL_REG: %02x\n",
  339. as3645a_read(flash, AS_CONTROL_REG));
  340. return rval & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
  341. }
  342. static int as3645a_detect(struct as3645a *flash)
  343. {
  344. struct device *dev = &flash->client->dev;
  345. int rval, man, model, rfu, version;
  346. const char *vendor;
  347. rval = as3645a_read(flash, AS_DESIGN_INFO_REG);
  348. if (rval < 0) {
  349. dev_err(dev, "can't read design info reg\n");
  350. return rval;
  351. }
  352. man = AS_DESIGN_INFO_FACTORY(rval);
  353. model = AS_DESIGN_INFO_MODEL(rval);
  354. rval = as3645a_read(flash, AS_VERSION_CONTROL_REG);
  355. if (rval < 0) {
  356. dev_err(dev, "can't read version control reg\n");
  357. return rval;
  358. }
  359. rfu = AS_VERSION_CONTROL_RFU(rval);
  360. version = AS_VERSION_CONTROL_VERSION(rval);
  361. /* Verify the chip model and version. */
  362. if (model != 0x01 || rfu != 0x00) {
  363. dev_err(dev, "AS3645A not detected (model %d rfu %d)\n",
  364. model, rfu);
  365. return -ENODEV;
  366. }
  367. switch (man) {
  368. case 1:
  369. vendor = "AMS, Austria Micro Systems";
  370. break;
  371. case 2:
  372. vendor = "ADI, Analog Devices Inc.";
  373. break;
  374. case 3:
  375. vendor = "NSC, National Semiconductor";
  376. break;
  377. case 4:
  378. vendor = "NXP";
  379. break;
  380. case 5:
  381. vendor = "TI, Texas Instrument";
  382. break;
  383. default:
  384. vendor = "Unknown";
  385. }
  386. dev_info(dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
  387. man, version);
  388. rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
  389. if (rval < 0)
  390. return rval;
  391. return as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
  392. }
  393. static int as3645a_parse_node(struct as3645a *flash,
  394. struct as3645a_names *names,
  395. struct device_node *node)
  396. {
  397. struct as3645a_config *cfg = &flash->cfg;
  398. struct device_node *child;
  399. const char *name;
  400. int rval;
  401. for_each_child_of_node(node, child) {
  402. u32 id = 0;
  403. of_property_read_u32(child, "reg", &id);
  404. switch (id) {
  405. case AS_LED_FLASH:
  406. flash->flash_node = of_node_get(child);
  407. break;
  408. case AS_LED_INDICATOR:
  409. flash->indicator_node = of_node_get(child);
  410. break;
  411. default:
  412. dev_warn(&flash->client->dev,
  413. "unknown LED %u encountered, ignoring\n", id);
  414. break;
  415. }
  416. }
  417. if (!flash->flash_node) {
  418. dev_err(&flash->client->dev, "can't find flash node\n");
  419. return -ENODEV;
  420. }
  421. rval = of_property_read_string(flash->flash_node, "label", &name);
  422. if (!rval)
  423. strlcpy(names->flash, name, sizeof(names->flash));
  424. else
  425. snprintf(names->flash, sizeof(names->flash),
  426. "%s:flash", node->name);
  427. rval = of_property_read_u32(flash->flash_node, "flash-timeout-us",
  428. &cfg->flash_timeout_us);
  429. if (rval < 0) {
  430. dev_err(&flash->client->dev,
  431. "can't read flash-timeout-us property for flash\n");
  432. goto out_err;
  433. }
  434. rval = of_property_read_u32(flash->flash_node, "flash-max-microamp",
  435. &cfg->flash_max_ua);
  436. if (rval < 0) {
  437. dev_err(&flash->client->dev,
  438. "can't read flash-max-microamp property for flash\n");
  439. goto out_err;
  440. }
  441. rval = of_property_read_u32(flash->flash_node, "led-max-microamp",
  442. &cfg->assist_max_ua);
  443. if (rval < 0) {
  444. dev_err(&flash->client->dev,
  445. "can't read led-max-microamp property for flash\n");
  446. goto out_err;
  447. }
  448. of_property_read_u32(flash->flash_node, "voltage-reference",
  449. &cfg->voltage_reference);
  450. of_property_read_u32(flash->flash_node, "ams,input-max-microamp",
  451. &cfg->peak);
  452. cfg->peak = AS_PEAK_mA_TO_REG(cfg->peak);
  453. if (!flash->indicator_node) {
  454. dev_warn(&flash->client->dev,
  455. "can't find indicator node\n");
  456. goto out_err;
  457. }
  458. rval = of_property_read_string(flash->indicator_node, "label", &name);
  459. if (!rval)
  460. strlcpy(names->indicator, name, sizeof(names->indicator));
  461. else
  462. snprintf(names->indicator, sizeof(names->indicator),
  463. "%s:indicator", node->name);
  464. rval = of_property_read_u32(flash->indicator_node, "led-max-microamp",
  465. &cfg->indicator_max_ua);
  466. if (rval < 0) {
  467. dev_err(&flash->client->dev,
  468. "can't read led-max-microamp property for indicator\n");
  469. goto out_err;
  470. }
  471. return 0;
  472. out_err:
  473. of_node_put(flash->flash_node);
  474. of_node_put(flash->indicator_node);
  475. return rval;
  476. }
  477. static int as3645a_led_class_setup(struct as3645a *flash,
  478. struct as3645a_names *names)
  479. {
  480. struct led_classdev *fled_cdev = &flash->fled.led_cdev;
  481. struct led_classdev *iled_cdev = &flash->iled_cdev;
  482. struct led_flash_setting *cfg;
  483. int rval;
  484. iled_cdev->name = names->indicator;
  485. iled_cdev->brightness_set_blocking = as3645a_set_indicator_brightness;
  486. iled_cdev->max_brightness =
  487. flash->cfg.indicator_max_ua / AS_INDICATOR_INTENSITY_STEP;
  488. iled_cdev->flags = LED_CORE_SUSPENDRESUME;
  489. rval = led_classdev_register(&flash->client->dev, iled_cdev);
  490. if (rval < 0)
  491. return rval;
  492. cfg = &flash->fled.brightness;
  493. cfg->min = AS_FLASH_INTENSITY_MIN;
  494. cfg->max = flash->cfg.flash_max_ua;
  495. cfg->step = AS_FLASH_INTENSITY_STEP;
  496. cfg->val = flash->cfg.flash_max_ua;
  497. cfg = &flash->fled.timeout;
  498. cfg->min = AS_FLASH_TIMEOUT_MIN;
  499. cfg->max = flash->cfg.flash_timeout_us;
  500. cfg->step = AS_FLASH_TIMEOUT_STEP;
  501. cfg->val = flash->cfg.flash_timeout_us;
  502. flash->fled.ops = &as3645a_led_flash_ops;
  503. fled_cdev->name = names->flash;
  504. fled_cdev->brightness_set_blocking = as3645a_set_assist_brightness;
  505. /* Value 0 is off in LED class. */
  506. fled_cdev->max_brightness =
  507. as3645a_current_to_reg(flash, false,
  508. flash->cfg.assist_max_ua) + 1;
  509. fled_cdev->flags = LED_DEV_CAP_FLASH | LED_CORE_SUSPENDRESUME;
  510. rval = led_classdev_flash_register(&flash->client->dev, &flash->fled);
  511. if (rval) {
  512. led_classdev_unregister(iled_cdev);
  513. dev_err(&flash->client->dev,
  514. "led_classdev_flash_register() failed, error %d\n",
  515. rval);
  516. }
  517. return rval;
  518. }
  519. static int as3645a_v4l2_setup(struct as3645a *flash)
  520. {
  521. struct led_classdev_flash *fled = &flash->fled;
  522. struct led_classdev *led = &fled->led_cdev;
  523. struct v4l2_flash_config cfg = {
  524. .intensity = {
  525. .min = AS_TORCH_INTENSITY_MIN,
  526. .max = flash->cfg.assist_max_ua,
  527. .step = AS_TORCH_INTENSITY_STEP,
  528. .val = flash->cfg.assist_max_ua,
  529. },
  530. };
  531. struct v4l2_flash_config cfgind = {
  532. .intensity = {
  533. .min = AS_INDICATOR_INTENSITY_MIN,
  534. .max = flash->cfg.indicator_max_ua,
  535. .step = AS_INDICATOR_INTENSITY_STEP,
  536. .val = flash->cfg.indicator_max_ua,
  537. },
  538. };
  539. strlcpy(cfg.dev_name, led->name, sizeof(cfg.dev_name));
  540. strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfg.dev_name));
  541. flash->vf = v4l2_flash_init(
  542. &flash->client->dev, of_fwnode_handle(flash->flash_node),
  543. &flash->fled, NULL, &cfg);
  544. if (IS_ERR(flash->vf))
  545. return PTR_ERR(flash->vf);
  546. flash->vfind = v4l2_flash_indicator_init(
  547. &flash->client->dev, of_fwnode_handle(flash->indicator_node),
  548. &flash->iled_cdev, &cfgind);
  549. if (IS_ERR(flash->vfind)) {
  550. v4l2_flash_release(flash->vf);
  551. return PTR_ERR(flash->vfind);
  552. }
  553. return 0;
  554. }
  555. static int as3645a_probe(struct i2c_client *client)
  556. {
  557. struct as3645a_names names;
  558. struct as3645a *flash;
  559. int rval;
  560. if (client->dev.of_node == NULL)
  561. return -ENODEV;
  562. flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
  563. if (flash == NULL)
  564. return -ENOMEM;
  565. flash->client = client;
  566. rval = as3645a_parse_node(flash, &names, client->dev.of_node);
  567. if (rval < 0)
  568. return rval;
  569. rval = as3645a_detect(flash);
  570. if (rval < 0)
  571. goto out_put_nodes;
  572. mutex_init(&flash->mutex);
  573. i2c_set_clientdata(client, flash);
  574. rval = as3645a_setup(flash);
  575. if (rval)
  576. goto out_mutex_destroy;
  577. rval = as3645a_led_class_setup(flash, &names);
  578. if (rval)
  579. goto out_mutex_destroy;
  580. rval = as3645a_v4l2_setup(flash);
  581. if (rval)
  582. goto out_led_classdev_flash_unregister;
  583. return 0;
  584. out_led_classdev_flash_unregister:
  585. led_classdev_flash_unregister(&flash->fled);
  586. out_mutex_destroy:
  587. mutex_destroy(&flash->mutex);
  588. out_put_nodes:
  589. of_node_put(flash->flash_node);
  590. of_node_put(flash->indicator_node);
  591. return rval;
  592. }
  593. static int as3645a_remove(struct i2c_client *client)
  594. {
  595. struct as3645a *flash = i2c_get_clientdata(client);
  596. as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
  597. v4l2_flash_release(flash->vf);
  598. v4l2_flash_release(flash->vfind);
  599. led_classdev_flash_unregister(&flash->fled);
  600. led_classdev_unregister(&flash->iled_cdev);
  601. mutex_destroy(&flash->mutex);
  602. of_node_put(flash->flash_node);
  603. of_node_put(flash->indicator_node);
  604. return 0;
  605. }
  606. static const struct i2c_device_id as3645a_id_table[] = {
  607. { AS_NAME, 0 },
  608. { },
  609. };
  610. MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
  611. static const struct of_device_id as3645a_of_table[] = {
  612. { .compatible = "ams,as3645a" },
  613. { },
  614. };
  615. MODULE_DEVICE_TABLE(of, as3645a_of_table);
  616. static struct i2c_driver as3645a_i2c_driver = {
  617. .driver = {
  618. .of_match_table = as3645a_of_table,
  619. .name = AS_NAME,
  620. },
  621. .probe_new = as3645a_probe,
  622. .remove = as3645a_remove,
  623. .id_table = as3645a_id_table,
  624. };
  625. module_i2c_driver(as3645a_i2c_driver);
  626. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  627. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
  628. MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones");
  629. MODULE_LICENSE("GPL v2");