leds-lp8860.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. * @client - Pointer to the I2C client
  88. * @led_dev - led class device pointer
  89. * @regmap - Devices register map
  90. * @eeprom_regmap - EEPROM register map
  91. * @enable_gpio - VDDIO/EN gpio to enable communication interface
  92. * @regulator - LED supply regulator pointer
  93. * @label - LED label
  94. **/
  95. struct lp8860_led {
  96. struct mutex lock;
  97. struct i2c_client *client;
  98. struct led_classdev led_dev;
  99. struct regmap *regmap;
  100. struct regmap *eeprom_regmap;
  101. struct gpio_desc *enable_gpio;
  102. struct regulator *regulator;
  103. const char *label;
  104. };
  105. struct lp8860_eeprom_reg {
  106. uint8_t reg;
  107. uint8_t value;
  108. };
  109. static struct lp8860_eeprom_reg lp8860_eeprom_disp_regs[] = {
  110. { LP8860_EEPROM_REG_0, 0xed },
  111. { LP8860_EEPROM_REG_1, 0xdf },
  112. { LP8860_EEPROM_REG_2, 0xdc },
  113. { LP8860_EEPROM_REG_3, 0xf0 },
  114. { LP8860_EEPROM_REG_4, 0xdf },
  115. { LP8860_EEPROM_REG_5, 0xe5 },
  116. { LP8860_EEPROM_REG_6, 0xf2 },
  117. { LP8860_EEPROM_REG_7, 0x77 },
  118. { LP8860_EEPROM_REG_8, 0x77 },
  119. { LP8860_EEPROM_REG_9, 0x71 },
  120. { LP8860_EEPROM_REG_10, 0x3f },
  121. { LP8860_EEPROM_REG_11, 0xb7 },
  122. { LP8860_EEPROM_REG_12, 0x17 },
  123. { LP8860_EEPROM_REG_13, 0xef },
  124. { LP8860_EEPROM_REG_14, 0xb0 },
  125. { LP8860_EEPROM_REG_15, 0x87 },
  126. { LP8860_EEPROM_REG_16, 0xce },
  127. { LP8860_EEPROM_REG_17, 0x72 },
  128. { LP8860_EEPROM_REG_18, 0xe5 },
  129. { LP8860_EEPROM_REG_19, 0xdf },
  130. { LP8860_EEPROM_REG_20, 0x35 },
  131. { LP8860_EEPROM_REG_21, 0x06 },
  132. { LP8860_EEPROM_REG_22, 0xdc },
  133. { LP8860_EEPROM_REG_23, 0x88 },
  134. { LP8860_EEPROM_REG_24, 0x3E },
  135. };
  136. static int lp8860_unlock_eeprom(struct lp8860_led *led, int lock)
  137. {
  138. int ret;
  139. mutex_lock(&led->lock);
  140. if (lock == LP8860_UNLOCK_EEPROM) {
  141. ret = regmap_write(led->regmap,
  142. LP8860_EEPROM_UNLOCK,
  143. LP8860_EEPROM_CODE_1);
  144. if (ret) {
  145. dev_err(&led->client->dev, "EEPROM Unlock failed\n");
  146. goto out;
  147. }
  148. ret = regmap_write(led->regmap,
  149. LP8860_EEPROM_UNLOCK,
  150. LP8860_EEPROM_CODE_2);
  151. if (ret) {
  152. dev_err(&led->client->dev, "EEPROM Unlock failed\n");
  153. goto out;
  154. }
  155. ret = regmap_write(led->regmap,
  156. LP8860_EEPROM_UNLOCK,
  157. LP8860_EEPROM_CODE_3);
  158. if (ret) {
  159. dev_err(&led->client->dev, "EEPROM Unlock failed\n");
  160. goto out;
  161. }
  162. } else {
  163. ret = regmap_write(led->regmap,
  164. LP8860_EEPROM_UNLOCK,
  165. LP8860_LOCK_EEPROM);
  166. }
  167. out:
  168. mutex_unlock(&led->lock);
  169. return ret;
  170. }
  171. static int lp8860_fault_check(struct lp8860_led *led)
  172. {
  173. int ret, fault;
  174. unsigned int read_buf;
  175. ret = regmap_read(led->regmap, LP8860_LED_FAULT, &read_buf);
  176. if (ret)
  177. goto out;
  178. fault = read_buf;
  179. ret = regmap_read(led->regmap, LP8860_FAULT, &read_buf);
  180. if (ret)
  181. goto out;
  182. fault |= read_buf;
  183. /* Attempt to clear any faults */
  184. if (fault)
  185. ret = regmap_write(led->regmap, LP8860_FAULT_CLEAR,
  186. LP8860_CLEAR_FAULTS);
  187. out:
  188. return ret;
  189. }
  190. static int lp8860_brightness_set(struct led_classdev *led_cdev,
  191. enum led_brightness brt_val)
  192. {
  193. struct lp8860_led *led =
  194. container_of(led_cdev, struct lp8860_led, led_dev);
  195. int disp_brightness = brt_val * 255;
  196. int ret;
  197. mutex_lock(&led->lock);
  198. ret = lp8860_fault_check(led);
  199. if (ret) {
  200. dev_err(&led->client->dev, "Cannot read/clear faults\n");
  201. goto out;
  202. }
  203. ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_MSB,
  204. (disp_brightness & 0xff00) >> 8);
  205. if (ret) {
  206. dev_err(&led->client->dev, "Cannot write CL1 MSB\n");
  207. goto out;
  208. }
  209. ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_LSB,
  210. disp_brightness & 0xff);
  211. if (ret) {
  212. dev_err(&led->client->dev, "Cannot write CL1 LSB\n");
  213. goto out;
  214. }
  215. out:
  216. mutex_unlock(&led->lock);
  217. return ret;
  218. }
  219. static int lp8860_init(struct lp8860_led *led)
  220. {
  221. unsigned int read_buf;
  222. int ret, i, reg_count;
  223. if (led->enable_gpio)
  224. gpiod_direction_output(led->enable_gpio, 1);
  225. ret = lp8860_fault_check(led);
  226. if (ret)
  227. goto out;
  228. ret = regmap_read(led->regmap, LP8860_STATUS, &read_buf);
  229. if (ret)
  230. goto out;
  231. ret = lp8860_unlock_eeprom(led, LP8860_UNLOCK_EEPROM);
  232. if (ret) {
  233. dev_err(&led->client->dev, "Failed unlocking EEPROM\n");
  234. goto out;
  235. }
  236. reg_count = ARRAY_SIZE(lp8860_eeprom_disp_regs) / sizeof(lp8860_eeprom_disp_regs[0]);
  237. for (i = 0; i < reg_count; i++) {
  238. ret = regmap_write(led->eeprom_regmap,
  239. lp8860_eeprom_disp_regs[i].reg,
  240. lp8860_eeprom_disp_regs[i].value);
  241. if (ret) {
  242. dev_err(&led->client->dev, "Failed writing EEPROM\n");
  243. goto out;
  244. }
  245. }
  246. ret = lp8860_unlock_eeprom(led, LP8860_LOCK_EEPROM);
  247. if (ret)
  248. goto out;
  249. ret = regmap_write(led->regmap,
  250. LP8860_EEPROM_CNTRL,
  251. LP8860_PROGRAM_EEPROM);
  252. if (ret)
  253. dev_err(&led->client->dev, "Failed programming EEPROM\n");
  254. out:
  255. if (ret)
  256. if (led->enable_gpio)
  257. gpiod_direction_output(led->enable_gpio, 0);
  258. return ret;
  259. }
  260. static const struct reg_default lp8860_reg_defs[] = {
  261. { LP8860_DISP_CL1_BRT_MSB, 0x00},
  262. { LP8860_DISP_CL1_BRT_LSB, 0x00},
  263. { LP8860_DISP_CL1_CURR_MSB, 0x00},
  264. { LP8860_DISP_CL1_CURR_LSB, 0x00},
  265. { LP8860_CL2_BRT_MSB, 0x00},
  266. { LP8860_CL2_BRT_LSB, 0x00},
  267. { LP8860_CL2_CURRENT, 0x00},
  268. { LP8860_CL3_BRT_MSB, 0x00},
  269. { LP8860_CL3_BRT_LSB, 0x00},
  270. { LP8860_CL3_CURRENT, 0x00},
  271. { LP8860_CL4_BRT_MSB, 0x00},
  272. { LP8860_CL4_BRT_LSB, 0x00},
  273. { LP8860_CL4_CURRENT, 0x00},
  274. { LP8860_CONFIG, 0x00},
  275. { LP8860_FAULT_CLEAR, 0x00},
  276. { LP8860_EEPROM_CNTRL, 0x80},
  277. { LP8860_EEPROM_UNLOCK, 0x00},
  278. };
  279. static const struct regmap_config lp8860_regmap_config = {
  280. .reg_bits = 8,
  281. .val_bits = 8,
  282. .max_register = LP8860_EEPROM_UNLOCK,
  283. .reg_defaults = lp8860_reg_defs,
  284. .num_reg_defaults = ARRAY_SIZE(lp8860_reg_defs),
  285. .cache_type = REGCACHE_NONE,
  286. };
  287. static const struct reg_default lp8860_eeprom_defs[] = {
  288. { LP8860_EEPROM_REG_0, 0x00 },
  289. { LP8860_EEPROM_REG_1, 0x00 },
  290. { LP8860_EEPROM_REG_2, 0x00 },
  291. { LP8860_EEPROM_REG_3, 0x00 },
  292. { LP8860_EEPROM_REG_4, 0x00 },
  293. { LP8860_EEPROM_REG_5, 0x00 },
  294. { LP8860_EEPROM_REG_6, 0x00 },
  295. { LP8860_EEPROM_REG_7, 0x00 },
  296. { LP8860_EEPROM_REG_8, 0x00 },
  297. { LP8860_EEPROM_REG_9, 0x00 },
  298. { LP8860_EEPROM_REG_10, 0x00 },
  299. { LP8860_EEPROM_REG_11, 0x00 },
  300. { LP8860_EEPROM_REG_12, 0x00 },
  301. { LP8860_EEPROM_REG_13, 0x00 },
  302. { LP8860_EEPROM_REG_14, 0x00 },
  303. { LP8860_EEPROM_REG_15, 0x00 },
  304. { LP8860_EEPROM_REG_16, 0x00 },
  305. { LP8860_EEPROM_REG_17, 0x00 },
  306. { LP8860_EEPROM_REG_18, 0x00 },
  307. { LP8860_EEPROM_REG_19, 0x00 },
  308. { LP8860_EEPROM_REG_20, 0x00 },
  309. { LP8860_EEPROM_REG_21, 0x00 },
  310. { LP8860_EEPROM_REG_22, 0x00 },
  311. { LP8860_EEPROM_REG_23, 0x00 },
  312. { LP8860_EEPROM_REG_24, 0x00 },
  313. };
  314. static const struct regmap_config lp8860_eeprom_regmap_config = {
  315. .reg_bits = 8,
  316. .val_bits = 8,
  317. .max_register = LP8860_EEPROM_REG_24,
  318. .reg_defaults = lp8860_eeprom_defs,
  319. .num_reg_defaults = ARRAY_SIZE(lp8860_eeprom_defs),
  320. .cache_type = REGCACHE_NONE,
  321. };
  322. static int lp8860_probe(struct i2c_client *client,
  323. const struct i2c_device_id *id)
  324. {
  325. int ret;
  326. struct lp8860_led *led;
  327. struct device_node *np = client->dev.of_node;
  328. led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
  329. if (!led)
  330. return -ENOMEM;
  331. led->label = LP8860_DISP_LED_NAME;
  332. if (client->dev.of_node) {
  333. ret = of_property_read_string(np, "label", &led->label);
  334. if (ret) {
  335. dev_err(&client->dev, "Missing label in dt\n");
  336. return -EINVAL;
  337. }
  338. }
  339. led->enable_gpio = devm_gpiod_get_optional(&client->dev,
  340. "enable", GPIOD_OUT_LOW);
  341. if (IS_ERR(led->enable_gpio)) {
  342. ret = PTR_ERR(led->enable_gpio);
  343. dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret);
  344. return ret;
  345. }
  346. led->regulator = devm_regulator_get(&client->dev, "vled");
  347. if (IS_ERR(led->regulator))
  348. led->regulator = NULL;
  349. led->client = client;
  350. led->led_dev.name = led->label;
  351. led->led_dev.max_brightness = LED_FULL;
  352. led->led_dev.brightness_set_blocking = lp8860_brightness_set;
  353. mutex_init(&led->lock);
  354. i2c_set_clientdata(client, led);
  355. led->regmap = devm_regmap_init_i2c(client, &lp8860_regmap_config);
  356. if (IS_ERR(led->regmap)) {
  357. ret = PTR_ERR(led->regmap);
  358. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  359. ret);
  360. return ret;
  361. }
  362. led->eeprom_regmap = devm_regmap_init_i2c(client, &lp8860_eeprom_regmap_config);
  363. if (IS_ERR(led->eeprom_regmap)) {
  364. ret = PTR_ERR(led->eeprom_regmap);
  365. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  366. ret);
  367. return ret;
  368. }
  369. ret = lp8860_init(led);
  370. if (ret)
  371. return ret;
  372. ret = led_classdev_register(&client->dev, &led->led_dev);
  373. if (ret) {
  374. dev_err(&client->dev, "led register err: %d\n", ret);
  375. return ret;
  376. }
  377. return 0;
  378. }
  379. static int lp8860_remove(struct i2c_client *client)
  380. {
  381. struct lp8860_led *led = i2c_get_clientdata(client);
  382. int ret;
  383. led_classdev_unregister(&led->led_dev);
  384. if (led->enable_gpio)
  385. gpiod_direction_output(led->enable_gpio, 0);
  386. if (led->regulator) {
  387. ret = regulator_disable(led->regulator);
  388. if (ret)
  389. dev_err(&led->client->dev,
  390. "Failed to disable regulator\n");
  391. }
  392. return 0;
  393. }
  394. static const struct i2c_device_id lp8860_id[] = {
  395. { "lp8860", 0 },
  396. { }
  397. };
  398. MODULE_DEVICE_TABLE(i2c, lp8860_id);
  399. #ifdef CONFIG_OF
  400. static const struct of_device_id of_lp8860_leds_match[] = {
  401. { .compatible = "ti,lp8860", },
  402. {},
  403. };
  404. MODULE_DEVICE_TABLE(of, of_lp8860_leds_match);
  405. #endif
  406. static struct i2c_driver lp8860_driver = {
  407. .driver = {
  408. .name = "lp8860",
  409. .of_match_table = of_match_ptr(of_lp8860_leds_match),
  410. },
  411. .probe = lp8860_probe,
  412. .remove = lp8860_remove,
  413. .id_table = lp8860_id,
  414. };
  415. module_i2c_driver(lp8860_driver);
  416. MODULE_DESCRIPTION("Texas Instruments LP8860 LED driver");
  417. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
  418. MODULE_LICENSE("GPL");