leds-as3645a.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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, brightness_ua);
  290. return as3645a_set_current(flash);
  291. }
  292. static int as3645a_set_flash_timeout(struct led_classdev_flash *fled,
  293. u32 timeout_us)
  294. {
  295. struct as3645a *flash = fled_to_as3645a(fled);
  296. flash->timeout = AS_TIMER_US_TO_CODE(timeout_us);
  297. return as3645a_set_timeout(flash);
  298. }
  299. static int as3645a_set_strobe(struct led_classdev_flash *fled, bool state)
  300. {
  301. struct as3645a *flash = fled_to_as3645a(fled);
  302. return as3645a_set_control(flash, AS_MODE_FLASH, state);
  303. }
  304. static const struct led_flash_ops as3645a_led_flash_ops = {
  305. .flash_brightness_set = as3645a_set_flash_brightness,
  306. .timeout_set = as3645a_set_flash_timeout,
  307. .strobe_set = as3645a_set_strobe,
  308. .fault_get = as3645a_get_fault,
  309. };
  310. static int as3645a_setup(struct as3645a *flash)
  311. {
  312. struct device *dev = &flash->client->dev;
  313. u32 fault = 0;
  314. int rval;
  315. /* clear errors */
  316. rval = as3645a_read(flash, AS_FAULT_INFO_REG);
  317. if (rval < 0)
  318. return rval;
  319. dev_dbg(dev, "Fault info: %02x\n", rval);
  320. rval = as3645a_set_current(flash);
  321. if (rval < 0)
  322. return rval;
  323. rval = as3645a_set_timeout(flash);
  324. if (rval < 0)
  325. return rval;
  326. rval = as3645a_set_control(flash, AS_MODE_INDICATOR, false);
  327. if (rval < 0)
  328. return rval;
  329. /* read status */
  330. rval = as3645a_get_fault(&flash->fled, &fault);
  331. if (rval < 0)
  332. return rval;
  333. dev_dbg(dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n",
  334. as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG));
  335. dev_dbg(dev, "AS_CURRENT_SET_REG: %02x\n",
  336. as3645a_read(flash, AS_CURRENT_SET_REG));
  337. dev_dbg(dev, "AS_CONTROL_REG: %02x\n",
  338. as3645a_read(flash, AS_CONTROL_REG));
  339. return rval & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
  340. }
  341. static int as3645a_detect(struct as3645a *flash)
  342. {
  343. struct device *dev = &flash->client->dev;
  344. int rval, man, model, rfu, version;
  345. const char *vendor;
  346. rval = as3645a_read(flash, AS_DESIGN_INFO_REG);
  347. if (rval < 0) {
  348. dev_err(dev, "can't read design info reg\n");
  349. return rval;
  350. }
  351. man = AS_DESIGN_INFO_FACTORY(rval);
  352. model = AS_DESIGN_INFO_MODEL(rval);
  353. rval = as3645a_read(flash, AS_VERSION_CONTROL_REG);
  354. if (rval < 0) {
  355. dev_err(dev, "can't read version control reg\n");
  356. return rval;
  357. }
  358. rfu = AS_VERSION_CONTROL_RFU(rval);
  359. version = AS_VERSION_CONTROL_VERSION(rval);
  360. /* Verify the chip model and version. */
  361. if (model != 0x01 || rfu != 0x00) {
  362. dev_err(dev, "AS3645A not detected "
  363. "(model %d rfu %d)\n", model, rfu);
  364. return -ENODEV;
  365. }
  366. switch (man) {
  367. case 1:
  368. vendor = "AMS, Austria Micro Systems";
  369. break;
  370. case 2:
  371. vendor = "ADI, Analog Devices Inc.";
  372. break;
  373. case 3:
  374. vendor = "NSC, National Semiconductor";
  375. break;
  376. case 4:
  377. vendor = "NXP";
  378. break;
  379. case 5:
  380. vendor = "TI, Texas Instrument";
  381. break;
  382. default:
  383. vendor = "Unknown";
  384. }
  385. dev_info(dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
  386. man, version);
  387. rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
  388. if (rval < 0)
  389. return rval;
  390. return as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
  391. }
  392. static int as3645a_parse_node(struct as3645a *flash,
  393. struct as3645a_names *names,
  394. struct device_node *node)
  395. {
  396. struct as3645a_config *cfg = &flash->cfg;
  397. struct device_node *child;
  398. const char *name;
  399. int rval;
  400. for_each_child_of_node(node, child) {
  401. u32 id = 0;
  402. of_property_read_u32(child, "reg", &id);
  403. switch (id) {
  404. case AS_LED_FLASH:
  405. flash->flash_node = of_node_get(child);
  406. break;
  407. case AS_LED_INDICATOR:
  408. flash->indicator_node = of_node_get(child);
  409. break;
  410. default:
  411. dev_warn(&flash->client->dev,
  412. "unknown LED %u encountered, ignoring\n", id);
  413. break;
  414. }
  415. }
  416. if (!flash->flash_node) {
  417. dev_err(&flash->client->dev, "can't find flash node\n");
  418. return -ENODEV;
  419. }
  420. rval = of_property_read_string(flash->flash_node, "label", &name);
  421. if (!rval)
  422. strlcpy(names->flash, name, sizeof(names->flash));
  423. else
  424. snprintf(names->flash, sizeof(names->flash),
  425. "%s:flash", node->name);
  426. rval = of_property_read_u32(flash->flash_node, "flash-timeout-us",
  427. &cfg->flash_timeout_us);
  428. if (rval < 0) {
  429. dev_err(&flash->client->dev,
  430. "can't read flash-timeout-us property for flash\n");
  431. goto out_err;
  432. }
  433. rval = of_property_read_u32(flash->flash_node, "flash-max-microamp",
  434. &cfg->flash_max_ua);
  435. if (rval < 0) {
  436. dev_err(&flash->client->dev,
  437. "can't read flash-max-microamp property for flash\n");
  438. goto out_err;
  439. }
  440. rval = of_property_read_u32(flash->flash_node, "led-max-microamp",
  441. &cfg->assist_max_ua);
  442. if (rval < 0) {
  443. dev_err(&flash->client->dev,
  444. "can't read led-max-microamp property for flash\n");
  445. goto out_err;
  446. }
  447. of_property_read_u32(flash->flash_node, "voltage-reference",
  448. &cfg->voltage_reference);
  449. of_property_read_u32(flash->flash_node, "ams,input-max-microamp",
  450. &cfg->peak);
  451. cfg->peak = AS_PEAK_mA_TO_REG(cfg->peak);
  452. if (!flash->indicator_node) {
  453. dev_warn(&flash->client->dev,
  454. "can't find indicator node\n");
  455. goto out_err;
  456. }
  457. rval = of_property_read_string(flash->indicator_node, "label", &name);
  458. if (!rval)
  459. strlcpy(names->indicator, name, sizeof(names->indicator));
  460. else
  461. snprintf(names->indicator, sizeof(names->indicator),
  462. "%s:indicator", node->name);
  463. rval = of_property_read_u32(flash->indicator_node, "led-max-microamp",
  464. &cfg->indicator_max_ua);
  465. if (rval < 0) {
  466. dev_err(&flash->client->dev,
  467. "can't read led-max-microamp property for indicator\n");
  468. goto out_err;
  469. }
  470. return 0;
  471. out_err:
  472. of_node_put(flash->flash_node);
  473. of_node_put(flash->indicator_node);
  474. return rval;
  475. }
  476. static int as3645a_led_class_setup(struct as3645a *flash,
  477. struct as3645a_names *names)
  478. {
  479. struct led_classdev *fled_cdev = &flash->fled.led_cdev;
  480. struct led_classdev *iled_cdev = &flash->iled_cdev;
  481. struct led_flash_setting *cfg;
  482. int rval;
  483. iled_cdev->name = names->indicator;
  484. iled_cdev->brightness_set_blocking = as3645a_set_indicator_brightness;
  485. iled_cdev->max_brightness =
  486. flash->cfg.indicator_max_ua / AS_INDICATOR_INTENSITY_STEP;
  487. iled_cdev->flags = LED_CORE_SUSPENDRESUME;
  488. rval = led_classdev_register(&flash->client->dev, iled_cdev);
  489. if (rval < 0)
  490. return rval;
  491. cfg = &flash->fled.brightness;
  492. cfg->min = AS_FLASH_INTENSITY_MIN;
  493. cfg->max = flash->cfg.flash_max_ua;
  494. cfg->step = AS_FLASH_INTENSITY_STEP;
  495. cfg->val = flash->cfg.flash_max_ua;
  496. cfg = &flash->fled.timeout;
  497. cfg->min = AS_FLASH_TIMEOUT_MIN;
  498. cfg->max = flash->cfg.flash_timeout_us;
  499. cfg->step = AS_FLASH_TIMEOUT_STEP;
  500. cfg->val = flash->cfg.flash_timeout_us;
  501. flash->fled.ops = &as3645a_led_flash_ops;
  502. fled_cdev->name = names->flash;
  503. fled_cdev->brightness_set_blocking = as3645a_set_assist_brightness;
  504. /* Value 0 is off in LED class. */
  505. fled_cdev->max_brightness =
  506. as3645a_current_to_reg(flash, false,
  507. flash->cfg.assist_max_ua) + 1;
  508. fled_cdev->flags = LED_DEV_CAP_FLASH | LED_CORE_SUSPENDRESUME;
  509. rval = led_classdev_flash_register(&flash->client->dev, &flash->fled);
  510. if (rval) {
  511. led_classdev_unregister(iled_cdev);
  512. dev_err(&flash->client->dev,
  513. "led_classdev_flash_register() failed, error %d\n",
  514. rval);
  515. }
  516. return rval;
  517. }
  518. static int as3645a_v4l2_setup(struct as3645a *flash)
  519. {
  520. struct led_classdev_flash *fled = &flash->fled;
  521. struct led_classdev *led = &fled->led_cdev;
  522. struct v4l2_flash_config cfg = {
  523. .intensity = {
  524. .min = AS_TORCH_INTENSITY_MIN,
  525. .max = flash->cfg.assist_max_ua,
  526. .step = AS_TORCH_INTENSITY_STEP,
  527. .val = flash->cfg.assist_max_ua,
  528. },
  529. };
  530. struct v4l2_flash_config cfgind = {
  531. .intensity = {
  532. .min = AS_INDICATOR_INTENSITY_MIN,
  533. .max = flash->cfg.indicator_max_ua,
  534. .step = AS_INDICATOR_INTENSITY_STEP,
  535. .val = flash->cfg.indicator_max_ua,
  536. },
  537. };
  538. strlcpy(cfg.dev_name, led->name, sizeof(cfg.dev_name));
  539. strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfg.dev_name));
  540. flash->vf = v4l2_flash_init(
  541. &flash->client->dev, of_fwnode_handle(flash->flash_node),
  542. &flash->fled, NULL, &cfg);
  543. if (IS_ERR(flash->vf))
  544. return PTR_ERR(flash->vf);
  545. flash->vfind = v4l2_flash_indicator_init(
  546. &flash->client->dev, of_fwnode_handle(flash->indicator_node),
  547. &flash->iled_cdev, &cfgind);
  548. if (IS_ERR(flash->vfind)) {
  549. v4l2_flash_release(flash->vf);
  550. return PTR_ERR(flash->vfind);
  551. }
  552. return 0;
  553. }
  554. static int as3645a_probe(struct i2c_client *client)
  555. {
  556. struct as3645a_names names;
  557. struct as3645a *flash;
  558. int rval;
  559. if (client->dev.of_node == NULL)
  560. return -ENODEV;
  561. flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
  562. if (flash == NULL)
  563. return -ENOMEM;
  564. flash->client = client;
  565. rval = as3645a_parse_node(flash, &names, client->dev.of_node);
  566. if (rval < 0)
  567. return rval;
  568. rval = as3645a_detect(flash);
  569. if (rval < 0)
  570. goto out_put_nodes;
  571. mutex_init(&flash->mutex);
  572. i2c_set_clientdata(client, flash);
  573. rval = as3645a_setup(flash);
  574. if (rval)
  575. goto out_mutex_destroy;
  576. rval = as3645a_led_class_setup(flash, &names);
  577. if (rval)
  578. goto out_mutex_destroy;
  579. rval = as3645a_v4l2_setup(flash);
  580. if (rval)
  581. goto out_led_classdev_flash_unregister;
  582. return 0;
  583. out_led_classdev_flash_unregister:
  584. led_classdev_flash_unregister(&flash->fled);
  585. out_mutex_destroy:
  586. mutex_destroy(&flash->mutex);
  587. out_put_nodes:
  588. of_node_put(flash->flash_node);
  589. of_node_put(flash->indicator_node);
  590. return rval;
  591. }
  592. static int as3645a_remove(struct i2c_client *client)
  593. {
  594. struct as3645a *flash = i2c_get_clientdata(client);
  595. as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
  596. v4l2_flash_release(flash->vf);
  597. v4l2_flash_release(flash->vfind);
  598. led_classdev_flash_unregister(&flash->fled);
  599. led_classdev_unregister(&flash->iled_cdev);
  600. mutex_destroy(&flash->mutex);
  601. of_node_put(flash->flash_node);
  602. of_node_put(flash->indicator_node);
  603. return 0;
  604. }
  605. static const struct i2c_device_id as3645a_id_table[] = {
  606. { AS_NAME, 0 },
  607. { },
  608. };
  609. MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
  610. static const struct of_device_id as3645a_of_table[] = {
  611. { .compatible = "ams,as3645a" },
  612. { },
  613. };
  614. MODULE_DEVICE_TABLE(of, as3645a_of_table);
  615. static struct i2c_driver as3645a_i2c_driver = {
  616. .driver = {
  617. .of_match_table = as3645a_of_table,
  618. .name = AS_NAME,
  619. },
  620. .probe_new = as3645a_probe,
  621. .remove = as3645a_remove,
  622. .id_table = as3645a_id_table,
  623. };
  624. module_i2c_driver(as3645a_i2c_driver);
  625. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  626. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
  627. MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones");
  628. MODULE_LICENSE("GPL v2");