leds-lp8860.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * TI LP8860 4-Channel LED Driver
  3. *
  4. * Copyright (C) 2014 Texas Instruments
  5. *
  6. * Author: Dan Murphy <dmurphy@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/i2c.h>
  14. #include <linux/init.h>
  15. #include <linux/leds.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/slab.h>
  24. #define LP8860_DISP_CL1_BRT_MSB 0x00
  25. #define LP8860_DISP_CL1_BRT_LSB 0x01
  26. #define LP8860_DISP_CL1_CURR_MSB 0x02
  27. #define LP8860_DISP_CL1_CURR_LSB 0x03
  28. #define LP8860_CL2_BRT_MSB 0x04
  29. #define LP8860_CL2_BRT_LSB 0x05
  30. #define LP8860_CL2_CURRENT 0x06
  31. #define LP8860_CL3_BRT_MSB 0x07
  32. #define LP8860_CL3_BRT_LSB 0x08
  33. #define LP8860_CL3_CURRENT 0x09
  34. #define LP8860_CL4_BRT_MSB 0x0a
  35. #define LP8860_CL4_BRT_LSB 0x0b
  36. #define LP8860_CL4_CURRENT 0x0c
  37. #define LP8860_CONFIG 0x0d
  38. #define LP8860_STATUS 0x0e
  39. #define LP8860_FAULT 0x0f
  40. #define LP8860_LED_FAULT 0x10
  41. #define LP8860_FAULT_CLEAR 0x11
  42. #define LP8860_ID 0x12
  43. #define LP8860_TEMP_MSB 0x13
  44. #define LP8860_TEMP_LSB 0x14
  45. #define LP8860_DISP_LED_CURR_MSB 0x15
  46. #define LP8860_DISP_LED_CURR_LSB 0x16
  47. #define LP8860_DISP_LED_PWM_MSB 0x17
  48. #define LP8860_DISP_LED_PWM_LSB 0x18
  49. #define LP8860_EEPROM_CNTRL 0x19
  50. #define LP8860_EEPROM_UNLOCK 0x1a
  51. #define LP8860_EEPROM_REG_0 0x60
  52. #define LP8860_EEPROM_REG_1 0x61
  53. #define LP8860_EEPROM_REG_2 0x62
  54. #define LP8860_EEPROM_REG_3 0x63
  55. #define LP8860_EEPROM_REG_4 0x64
  56. #define LP8860_EEPROM_REG_5 0x65
  57. #define LP8860_EEPROM_REG_6 0x66
  58. #define LP8860_EEPROM_REG_7 0x67
  59. #define LP8860_EEPROM_REG_8 0x68
  60. #define LP8860_EEPROM_REG_9 0x69
  61. #define LP8860_EEPROM_REG_10 0x6a
  62. #define LP8860_EEPROM_REG_11 0x6b
  63. #define LP8860_EEPROM_REG_12 0x6c
  64. #define LP8860_EEPROM_REG_13 0x6d
  65. #define LP8860_EEPROM_REG_14 0x6e
  66. #define LP8860_EEPROM_REG_15 0x6f
  67. #define LP8860_EEPROM_REG_16 0x70
  68. #define LP8860_EEPROM_REG_17 0x71
  69. #define LP8860_EEPROM_REG_18 0x72
  70. #define LP8860_EEPROM_REG_19 0x73
  71. #define LP8860_EEPROM_REG_20 0x74
  72. #define LP8860_EEPROM_REG_21 0x75
  73. #define LP8860_EEPROM_REG_22 0x76
  74. #define LP8860_EEPROM_REG_23 0x77
  75. #define LP8860_EEPROM_REG_24 0x78
  76. #define LP8860_LOCK_EEPROM 0x00
  77. #define LP8860_UNLOCK_EEPROM 0x01
  78. #define LP8860_PROGRAM_EEPROM 0x02
  79. #define LP8860_EEPROM_CODE_1 0x08
  80. #define LP8860_EEPROM_CODE_2 0xba
  81. #define LP8860_EEPROM_CODE_3 0xef
  82. #define LP8860_CLEAR_FAULTS 0x01
  83. #define LP8860_DISP_LED_NAME "display_cluster"
  84. /**
  85. * struct lp8860_led -
  86. * @lock - Lock for reading/writing the device
  87. * @work - Work item used to off load the brightness register writes
  88. * @client - Pointer to the I2C client
  89. * @led_dev - led class device pointer
  90. * @regmap - Devices register map
  91. * @eeprom_regmap - EEPROM register map
  92. * @enable_gpio - VDDIO/EN gpio to enable communication interface
  93. * @regulator - LED supply regulator pointer
  94. * @brightness - Current brightness value requested
  95. * @label - LED label
  96. **/
  97. struct lp8860_led {
  98. struct mutex lock;
  99. struct work_struct work;
  100. struct i2c_client *client;
  101. struct led_classdev led_dev;
  102. struct regmap *regmap;
  103. struct regmap *eeprom_regmap;
  104. struct gpio_desc *enable_gpio;
  105. struct regulator *regulator;
  106. enum led_brightness brightness;
  107. const char *label;
  108. };
  109. struct lp8860_eeprom_reg {
  110. uint8_t reg;
  111. uint8_t value;
  112. };
  113. static struct lp8860_eeprom_reg lp8860_eeprom_disp_regs[] = {
  114. { LP8860_EEPROM_REG_0, 0xed },
  115. { LP8860_EEPROM_REG_1, 0xdf },
  116. { LP8860_EEPROM_REG_2, 0xdc },
  117. { LP8860_EEPROM_REG_3, 0xf0 },
  118. { LP8860_EEPROM_REG_4, 0xdf },
  119. { LP8860_EEPROM_REG_5, 0xe5 },
  120. { LP8860_EEPROM_REG_6, 0xf2 },
  121. { LP8860_EEPROM_REG_7, 0x77 },
  122. { LP8860_EEPROM_REG_8, 0x77 },
  123. { LP8860_EEPROM_REG_9, 0x71 },
  124. { LP8860_EEPROM_REG_10, 0x3f },
  125. { LP8860_EEPROM_REG_11, 0xb7 },
  126. { LP8860_EEPROM_REG_12, 0x17 },
  127. { LP8860_EEPROM_REG_13, 0xef },
  128. { LP8860_EEPROM_REG_14, 0xb0 },
  129. { LP8860_EEPROM_REG_15, 0x87 },
  130. { LP8860_EEPROM_REG_16, 0xce },
  131. { LP8860_EEPROM_REG_17, 0x72 },
  132. { LP8860_EEPROM_REG_18, 0xe5 },
  133. { LP8860_EEPROM_REG_19, 0xdf },
  134. { LP8860_EEPROM_REG_20, 0x35 },
  135. { LP8860_EEPROM_REG_21, 0x06 },
  136. { LP8860_EEPROM_REG_22, 0xdc },
  137. { LP8860_EEPROM_REG_23, 0x88 },
  138. { LP8860_EEPROM_REG_24, 0x3E },
  139. };
  140. static int lp8860_unlock_eeprom(struct lp8860_led *led, int lock)
  141. {
  142. int ret;
  143. mutex_lock(&led->lock);
  144. if (lock == LP8860_UNLOCK_EEPROM) {
  145. ret = regmap_write(led->regmap,
  146. LP8860_EEPROM_UNLOCK,
  147. LP8860_EEPROM_CODE_1);
  148. if (ret) {
  149. dev_err(&led->client->dev, "EEPROM Unlock failed\n");
  150. goto out;
  151. }
  152. ret = regmap_write(led->regmap,
  153. LP8860_EEPROM_UNLOCK,
  154. LP8860_EEPROM_CODE_2);
  155. if (ret) {
  156. dev_err(&led->client->dev, "EEPROM Unlock failed\n");
  157. goto out;
  158. }
  159. ret = regmap_write(led->regmap,
  160. LP8860_EEPROM_UNLOCK,
  161. LP8860_EEPROM_CODE_3);
  162. if (ret) {
  163. dev_err(&led->client->dev, "EEPROM Unlock failed\n");
  164. goto out;
  165. }
  166. } else {
  167. ret = regmap_write(led->regmap,
  168. LP8860_EEPROM_UNLOCK,
  169. LP8860_LOCK_EEPROM);
  170. }
  171. out:
  172. mutex_unlock(&led->lock);
  173. return ret;
  174. }
  175. static int lp8860_fault_check(struct lp8860_led *led)
  176. {
  177. int ret, fault;
  178. unsigned int read_buf;
  179. ret = regmap_read(led->regmap, LP8860_LED_FAULT, &read_buf);
  180. if (ret)
  181. goto out;
  182. fault = read_buf;
  183. ret = regmap_read(led->regmap, LP8860_FAULT, &read_buf);
  184. if (ret)
  185. goto out;
  186. fault |= read_buf;
  187. /* Attempt to clear any faults */
  188. if (fault)
  189. ret = regmap_write(led->regmap, LP8860_FAULT_CLEAR,
  190. LP8860_CLEAR_FAULTS);
  191. out:
  192. return ret;
  193. }
  194. static void lp8860_led_brightness_work(struct work_struct *work)
  195. {
  196. struct lp8860_led *led = container_of(work, struct lp8860_led, work);
  197. int ret;
  198. int disp_brightness = led->brightness * 255;
  199. mutex_lock(&led->lock);
  200. ret = lp8860_fault_check(led);
  201. if (ret) {
  202. dev_err(&led->client->dev, "Cannot read/clear faults\n");
  203. goto out;
  204. }
  205. ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_MSB,
  206. (disp_brightness & 0xff00) >> 8);
  207. if (ret) {
  208. dev_err(&led->client->dev, "Cannot write CL1 MSB\n");
  209. goto out;
  210. }
  211. ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_LSB,
  212. disp_brightness & 0xff);
  213. if (ret) {
  214. dev_err(&led->client->dev, "Cannot write CL1 LSB\n");
  215. goto out;
  216. }
  217. out:
  218. mutex_unlock(&led->lock);
  219. }
  220. static void lp8860_brightness_set(struct led_classdev *led_cdev,
  221. enum led_brightness brt_val)
  222. {
  223. struct lp8860_led *led =
  224. container_of(led_cdev, struct lp8860_led, led_dev);
  225. led->brightness = brt_val;
  226. schedule_work(&led->work);
  227. }
  228. static int lp8860_init(struct lp8860_led *led)
  229. {
  230. unsigned int read_buf;
  231. int ret, i, reg_count;
  232. if (led->enable_gpio)
  233. gpiod_direction_output(led->enable_gpio, 1);
  234. ret = lp8860_fault_check(led);
  235. if (ret)
  236. goto out;
  237. ret = regmap_read(led->regmap, LP8860_STATUS, &read_buf);
  238. if (ret)
  239. goto out;
  240. ret = lp8860_unlock_eeprom(led, LP8860_UNLOCK_EEPROM);
  241. if (ret) {
  242. dev_err(&led->client->dev, "Failed unlocking EEPROM\n");
  243. goto out;
  244. }
  245. reg_count = ARRAY_SIZE(lp8860_eeprom_disp_regs) / sizeof(lp8860_eeprom_disp_regs[0]);
  246. for (i = 0; i < reg_count; i++) {
  247. ret = regmap_write(led->eeprom_regmap,
  248. lp8860_eeprom_disp_regs[i].reg,
  249. lp8860_eeprom_disp_regs[i].value);
  250. if (ret) {
  251. dev_err(&led->client->dev, "Failed writing EEPROM\n");
  252. goto out;
  253. }
  254. }
  255. ret = lp8860_unlock_eeprom(led, LP8860_LOCK_EEPROM);
  256. if (ret)
  257. goto out;
  258. ret = regmap_write(led->regmap,
  259. LP8860_EEPROM_CNTRL,
  260. LP8860_PROGRAM_EEPROM);
  261. if (ret)
  262. dev_err(&led->client->dev, "Failed programming EEPROM\n");
  263. out:
  264. if (ret)
  265. if (led->enable_gpio)
  266. gpiod_direction_output(led->enable_gpio, 0);
  267. return ret;
  268. }
  269. static const struct reg_default lp8860_reg_defs[] = {
  270. { LP8860_DISP_CL1_BRT_MSB, 0x00},
  271. { LP8860_DISP_CL1_BRT_LSB, 0x00},
  272. { LP8860_DISP_CL1_CURR_MSB, 0x00},
  273. { LP8860_DISP_CL1_CURR_LSB, 0x00},
  274. { LP8860_CL2_BRT_MSB, 0x00},
  275. { LP8860_CL2_BRT_LSB, 0x00},
  276. { LP8860_CL2_CURRENT, 0x00},
  277. { LP8860_CL3_BRT_MSB, 0x00},
  278. { LP8860_CL3_BRT_LSB, 0x00},
  279. { LP8860_CL3_CURRENT, 0x00},
  280. { LP8860_CL4_BRT_MSB, 0x00},
  281. { LP8860_CL4_BRT_LSB, 0x00},
  282. { LP8860_CL4_CURRENT, 0x00},
  283. { LP8860_CONFIG, 0x00},
  284. { LP8860_FAULT_CLEAR, 0x00},
  285. { LP8860_EEPROM_CNTRL, 0x80},
  286. { LP8860_EEPROM_UNLOCK, 0x00},
  287. };
  288. static const struct regmap_config lp8860_regmap_config = {
  289. .reg_bits = 8,
  290. .val_bits = 8,
  291. .max_register = LP8860_EEPROM_UNLOCK,
  292. .reg_defaults = lp8860_reg_defs,
  293. .num_reg_defaults = ARRAY_SIZE(lp8860_reg_defs),
  294. .cache_type = REGCACHE_NONE,
  295. };
  296. static const struct reg_default lp8860_eeprom_defs[] = {
  297. { LP8860_EEPROM_REG_0, 0x00 },
  298. { LP8860_EEPROM_REG_1, 0x00 },
  299. { LP8860_EEPROM_REG_2, 0x00 },
  300. { LP8860_EEPROM_REG_3, 0x00 },
  301. { LP8860_EEPROM_REG_4, 0x00 },
  302. { LP8860_EEPROM_REG_5, 0x00 },
  303. { LP8860_EEPROM_REG_6, 0x00 },
  304. { LP8860_EEPROM_REG_7, 0x00 },
  305. { LP8860_EEPROM_REG_8, 0x00 },
  306. { LP8860_EEPROM_REG_9, 0x00 },
  307. { LP8860_EEPROM_REG_10, 0x00 },
  308. { LP8860_EEPROM_REG_11, 0x00 },
  309. { LP8860_EEPROM_REG_12, 0x00 },
  310. { LP8860_EEPROM_REG_13, 0x00 },
  311. { LP8860_EEPROM_REG_14, 0x00 },
  312. { LP8860_EEPROM_REG_15, 0x00 },
  313. { LP8860_EEPROM_REG_16, 0x00 },
  314. { LP8860_EEPROM_REG_17, 0x00 },
  315. { LP8860_EEPROM_REG_18, 0x00 },
  316. { LP8860_EEPROM_REG_19, 0x00 },
  317. { LP8860_EEPROM_REG_20, 0x00 },
  318. { LP8860_EEPROM_REG_21, 0x00 },
  319. { LP8860_EEPROM_REG_22, 0x00 },
  320. { LP8860_EEPROM_REG_23, 0x00 },
  321. { LP8860_EEPROM_REG_24, 0x00 },
  322. };
  323. static const struct regmap_config lp8860_eeprom_regmap_config = {
  324. .reg_bits = 8,
  325. .val_bits = 8,
  326. .max_register = LP8860_EEPROM_REG_24,
  327. .reg_defaults = lp8860_eeprom_defs,
  328. .num_reg_defaults = ARRAY_SIZE(lp8860_eeprom_defs),
  329. .cache_type = REGCACHE_NONE,
  330. };
  331. static int lp8860_probe(struct i2c_client *client,
  332. const struct i2c_device_id *id)
  333. {
  334. int ret;
  335. struct lp8860_led *led;
  336. struct device_node *np = client->dev.of_node;
  337. led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
  338. if (!led)
  339. return -ENOMEM;
  340. led->label = LP8860_DISP_LED_NAME;
  341. if (client->dev.of_node) {
  342. ret = of_property_read_string(np, "label", &led->label);
  343. if (ret) {
  344. dev_err(&client->dev, "Missing label in dt\n");
  345. return -EINVAL;
  346. }
  347. }
  348. led->enable_gpio = devm_gpiod_get_optional(&client->dev,
  349. "enable", GPIOD_OUT_LOW);
  350. if (IS_ERR(led->enable_gpio)) {
  351. ret = PTR_ERR(led->enable_gpio);
  352. dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret);
  353. return ret;
  354. }
  355. led->regulator = devm_regulator_get(&client->dev, "vled");
  356. if (IS_ERR(led->regulator))
  357. led->regulator = NULL;
  358. led->client = client;
  359. led->led_dev.name = led->label;
  360. led->led_dev.max_brightness = LED_FULL;
  361. led->led_dev.brightness_set = lp8860_brightness_set;
  362. mutex_init(&led->lock);
  363. INIT_WORK(&led->work, lp8860_led_brightness_work);
  364. i2c_set_clientdata(client, led);
  365. led->regmap = devm_regmap_init_i2c(client, &lp8860_regmap_config);
  366. if (IS_ERR(led->regmap)) {
  367. ret = PTR_ERR(led->regmap);
  368. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  369. ret);
  370. return ret;
  371. }
  372. led->eeprom_regmap = devm_regmap_init_i2c(client, &lp8860_eeprom_regmap_config);
  373. if (IS_ERR(led->eeprom_regmap)) {
  374. ret = PTR_ERR(led->eeprom_regmap);
  375. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  376. ret);
  377. return ret;
  378. }
  379. ret = lp8860_init(led);
  380. if (ret)
  381. return ret;
  382. ret = led_classdev_register(&client->dev, &led->led_dev);
  383. if (ret) {
  384. dev_err(&client->dev, "led register err: %d\n", ret);
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. static int lp8860_remove(struct i2c_client *client)
  390. {
  391. struct lp8860_led *led = i2c_get_clientdata(client);
  392. int ret;
  393. led_classdev_unregister(&led->led_dev);
  394. cancel_work_sync(&led->work);
  395. if (led->enable_gpio)
  396. gpiod_direction_output(led->enable_gpio, 0);
  397. if (led->regulator) {
  398. ret = regulator_disable(led->regulator);
  399. if (ret)
  400. dev_err(&led->client->dev,
  401. "Failed to disable regulator\n");
  402. }
  403. return 0;
  404. }
  405. static const struct i2c_device_id lp8860_id[] = {
  406. { "lp8860", 0 },
  407. { }
  408. };
  409. MODULE_DEVICE_TABLE(i2c, lp8860_id);
  410. #ifdef CONFIG_OF
  411. static const struct of_device_id of_lp8860_leds_match[] = {
  412. { .compatible = "ti,lp8860", },
  413. {},
  414. };
  415. MODULE_DEVICE_TABLE(of, of_lp8860_leds_match);
  416. #endif
  417. static struct i2c_driver lp8860_driver = {
  418. .driver = {
  419. .name = "lp8860",
  420. .of_match_table = of_match_ptr(of_lp8860_leds_match),
  421. },
  422. .probe = lp8860_probe,
  423. .remove = lp8860_remove,
  424. .id_table = lp8860_id,
  425. };
  426. module_i2c_driver(lp8860_driver);
  427. MODULE_DESCRIPTION("Texas Instruments LP8860 LED driver");
  428. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
  429. MODULE_LICENSE("GPL");