leds-lm3692x.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * TI lm3692x LED Driver
  3. *
  4. * Copyright (C) 2017 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. * Data sheet is located
  13. * http://www.ti.com/lit/ds/snvsa29/snvsa29.pdf
  14. */
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/i2c.h>
  17. #include <linux/init.h>
  18. #include <linux/leds.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/of.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/regmap.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/slab.h>
  26. #include <uapi/linux/uleds.h>
  27. #define LM3692X_REV 0x0
  28. #define LM3692X_RESET 0x1
  29. #define LM3692X_EN 0x10
  30. #define LM3692X_BRT_CTRL 0x11
  31. #define LM3692X_PWM_CTRL 0x12
  32. #define LM3692X_BOOST_CTRL 0x13
  33. #define LM3692X_AUTO_FREQ_HI 0x15
  34. #define LM3692X_AUTO_FREQ_LO 0x16
  35. #define LM3692X_BL_ADJ_THRESH 0x17
  36. #define LM3692X_BRT_LSB 0x18
  37. #define LM3692X_BRT_MSB 0x19
  38. #define LM3692X_FAULT_CTRL 0x1e
  39. #define LM3692X_FAULT_FLAGS 0x1f
  40. #define LM3692X_SW_RESET BIT(0)
  41. #define LM3692X_DEVICE_EN BIT(0)
  42. #define LM3692X_LED1_EN BIT(1)
  43. #define LM3692X_LED2_EN BIT(2)
  44. /* Brightness Control Bits */
  45. #define LM3692X_BL_ADJ_POL BIT(0)
  46. #define LM3692X_RAMP_RATE_125us 0x00
  47. #define LM3692X_RAMP_RATE_250us BIT(1)
  48. #define LM3692X_RAMP_RATE_500us BIT(2)
  49. #define LM3692X_RAMP_RATE_1ms (BIT(1) | BIT(2))
  50. #define LM3692X_RAMP_RATE_2ms BIT(3)
  51. #define LM3692X_RAMP_RATE_4ms (BIT(3) | BIT(1))
  52. #define LM3692X_RAMP_RATE_8ms (BIT(2) | BIT(3))
  53. #define LM3692X_RAMP_RATE_16ms (BIT(1) | BIT(2) | BIT(3))
  54. #define LM3692X_RAMP_EN BIT(4)
  55. #define LM3692X_BRHT_MODE_REG 0x00
  56. #define LM3692X_BRHT_MODE_PWM BIT(5)
  57. #define LM3692X_BRHT_MODE_MULTI_RAMP BIT(6)
  58. #define LM3692X_BRHT_MODE_RAMP_MULTI (BIT(5) | BIT(6))
  59. #define LM3692X_MAP_MODE_EXP BIT(7)
  60. /* PWM Register Bits */
  61. #define LM3692X_PWM_FILTER_100 BIT(0)
  62. #define LM3692X_PWM_FILTER_150 BIT(1)
  63. #define LM3692X_PWM_FILTER_200 (BIT(0) | BIT(1))
  64. #define LM3692X_PWM_HYSTER_1LSB BIT(2)
  65. #define LM3692X_PWM_HYSTER_2LSB BIT(3)
  66. #define LM3692X_PWM_HYSTER_3LSB (BIT(3) | BIT(2))
  67. #define LM3692X_PWM_HYSTER_4LSB BIT(4)
  68. #define LM3692X_PWM_HYSTER_5LSB (BIT(4) | BIT(2))
  69. #define LM3692X_PWM_HYSTER_6LSB (BIT(4) | BIT(3))
  70. #define LM3692X_PWM_POLARITY BIT(5)
  71. #define LM3692X_PWM_SAMP_4MHZ BIT(6)
  72. #define LM3692X_PWM_SAMP_24MHZ BIT(7)
  73. /* Boost Control Bits */
  74. #define LM3692X_OCP_PROT_1A BIT(0)
  75. #define LM3692X_OCP_PROT_1_25A BIT(1)
  76. #define LM3692X_OCP_PROT_1_5A (BIT(0) | BIT(1))
  77. #define LM3692X_OVP_21V BIT(2)
  78. #define LM3692X_OVP_25V BIT(3)
  79. #define LM3692X_OVP_29V (BIT(2) | BIT(3))
  80. #define LM3692X_MIN_IND_22UH BIT(4)
  81. #define LM3692X_BOOST_SW_1MHZ BIT(5)
  82. #define LM3692X_BOOST_SW_NO_SHIFT BIT(6)
  83. /* Fault Control Bits */
  84. #define LM3692X_FAULT_CTRL_OVP BIT(0)
  85. #define LM3692X_FAULT_CTRL_OCP BIT(1)
  86. #define LM3692X_FAULT_CTRL_TSD BIT(2)
  87. #define LM3692X_FAULT_CTRL_OPEN BIT(3)
  88. /* Fault Flag Bits */
  89. #define LM3692X_FAULT_FLAG_OVP BIT(0)
  90. #define LM3692X_FAULT_FLAG_OCP BIT(1)
  91. #define LM3692X_FAULT_FLAG_TSD BIT(2)
  92. #define LM3692X_FAULT_FLAG_SHRT BIT(3)
  93. #define LM3692X_FAULT_FLAG_OPEN BIT(4)
  94. /**
  95. * struct lm3692x_led -
  96. * @lock - Lock for reading/writing the device
  97. * @client - Pointer to the I2C client
  98. * @led_dev - LED class device pointer
  99. * @regmap - Devices register map
  100. * @enable_gpio - VDDIO/EN gpio to enable communication interface
  101. * @regulator - LED supply regulator pointer
  102. * @label - LED label
  103. */
  104. struct lm3692x_led {
  105. struct mutex lock;
  106. struct i2c_client *client;
  107. struct led_classdev led_dev;
  108. struct regmap *regmap;
  109. struct gpio_desc *enable_gpio;
  110. struct regulator *regulator;
  111. char label[LED_MAX_NAME_SIZE];
  112. };
  113. static const struct reg_default lm3692x_reg_defs[] = {
  114. {LM3692X_EN, 0xf},
  115. {LM3692X_BRT_CTRL, 0x61},
  116. {LM3692X_PWM_CTRL, 0x73},
  117. {LM3692X_BOOST_CTRL, 0x6f},
  118. {LM3692X_AUTO_FREQ_HI, 0x0},
  119. {LM3692X_AUTO_FREQ_LO, 0x0},
  120. {LM3692X_BL_ADJ_THRESH, 0x0},
  121. {LM3692X_BRT_LSB, 0x7},
  122. {LM3692X_BRT_MSB, 0xff},
  123. {LM3692X_FAULT_CTRL, 0x7},
  124. };
  125. static const struct regmap_config lm3692x_regmap_config = {
  126. .reg_bits = 8,
  127. .val_bits = 8,
  128. .max_register = LM3692X_FAULT_FLAGS,
  129. .reg_defaults = lm3692x_reg_defs,
  130. .num_reg_defaults = ARRAY_SIZE(lm3692x_reg_defs),
  131. .cache_type = REGCACHE_RBTREE,
  132. };
  133. static int lm3692x_fault_check(struct lm3692x_led *led)
  134. {
  135. int ret;
  136. unsigned int read_buf;
  137. ret = regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf);
  138. if (ret)
  139. return ret;
  140. if (read_buf)
  141. dev_err(&led->client->dev, "Detected a fault 0x%X\n", read_buf);
  142. /* The first read may clear the fault. Check again to see if the fault
  143. * still exits and return that value.
  144. */
  145. regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf);
  146. if (read_buf)
  147. dev_err(&led->client->dev, "Second read of fault flags 0x%X\n",
  148. read_buf);
  149. return read_buf;
  150. }
  151. static int lm3692x_brightness_set(struct led_classdev *led_cdev,
  152. enum led_brightness brt_val)
  153. {
  154. struct lm3692x_led *led =
  155. container_of(led_cdev, struct lm3692x_led, led_dev);
  156. int ret;
  157. int led_brightness_lsb = (brt_val >> 5);
  158. mutex_lock(&led->lock);
  159. ret = lm3692x_fault_check(led);
  160. if (ret) {
  161. dev_err(&led->client->dev, "Cannot read/clear faults\n");
  162. goto out;
  163. }
  164. ret = regmap_write(led->regmap, LM3692X_BRT_MSB, brt_val);
  165. if (ret) {
  166. dev_err(&led->client->dev, "Cannot write MSB\n");
  167. goto out;
  168. }
  169. ret = regmap_write(led->regmap, LM3692X_BRT_LSB, led_brightness_lsb);
  170. if (ret) {
  171. dev_err(&led->client->dev, "Cannot write LSB\n");
  172. goto out;
  173. }
  174. out:
  175. mutex_unlock(&led->lock);
  176. return ret;
  177. }
  178. static int lm3692x_init(struct lm3692x_led *led)
  179. {
  180. int ret;
  181. if (led->regulator) {
  182. ret = regulator_enable(led->regulator);
  183. if (ret) {
  184. dev_err(&led->client->dev,
  185. "Failed to enable regulator\n");
  186. return ret;
  187. }
  188. }
  189. if (led->enable_gpio)
  190. gpiod_direction_output(led->enable_gpio, 1);
  191. ret = lm3692x_fault_check(led);
  192. if (ret) {
  193. dev_err(&led->client->dev, "Cannot read/clear faults\n");
  194. goto out;
  195. }
  196. ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, 0x00);
  197. if (ret)
  198. goto out;
  199. /*
  200. * For glitch free operation, the following data should
  201. * only be written while device enable bit is 0
  202. * per Section 7.5.14 of the data sheet
  203. */
  204. ret = regmap_write(led->regmap, LM3692X_PWM_CTRL,
  205. LM3692X_PWM_FILTER_100 | LM3692X_PWM_SAMP_24MHZ);
  206. if (ret)
  207. goto out;
  208. ret = regmap_write(led->regmap, LM3692X_BOOST_CTRL,
  209. LM3692X_BRHT_MODE_RAMP_MULTI |
  210. LM3692X_BL_ADJ_POL |
  211. LM3692X_RAMP_RATE_250us);
  212. if (ret)
  213. goto out;
  214. ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_HI, 0x00);
  215. if (ret)
  216. goto out;
  217. ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_LO, 0x00);
  218. if (ret)
  219. goto out;
  220. ret = regmap_write(led->regmap, LM3692X_BL_ADJ_THRESH, 0x00);
  221. if (ret)
  222. goto out;
  223. ret = regmap_write(led->regmap, LM3692X_BRT_CTRL,
  224. LM3692X_BL_ADJ_POL | LM3692X_PWM_HYSTER_4LSB);
  225. if (ret)
  226. goto out;
  227. return ret;
  228. out:
  229. dev_err(&led->client->dev, "Fail writing initialization values\n");
  230. if (led->enable_gpio)
  231. gpiod_direction_output(led->enable_gpio, 0);
  232. if (led->regulator) {
  233. ret = regulator_disable(led->regulator);
  234. if (ret)
  235. dev_err(&led->client->dev,
  236. "Failed to disable regulator\n");
  237. }
  238. return ret;
  239. }
  240. static int lm3692x_probe(struct i2c_client *client,
  241. const struct i2c_device_id *id)
  242. {
  243. int ret;
  244. struct lm3692x_led *led;
  245. struct device_node *np = client->dev.of_node;
  246. struct device_node *child_node;
  247. const char *name;
  248. led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
  249. if (!led)
  250. return -ENOMEM;
  251. for_each_available_child_of_node(np, child_node) {
  252. led->led_dev.default_trigger = of_get_property(child_node,
  253. "linux,default-trigger",
  254. NULL);
  255. ret = of_property_read_string(child_node, "label", &name);
  256. if (!ret)
  257. snprintf(led->label, sizeof(led->label),
  258. "%s:%s", id->name, name);
  259. else
  260. snprintf(led->label, sizeof(led->label),
  261. "%s::backlight_cluster", id->name);
  262. };
  263. led->enable_gpio = devm_gpiod_get_optional(&client->dev,
  264. "enable", GPIOD_OUT_LOW);
  265. if (IS_ERR(led->enable_gpio)) {
  266. ret = PTR_ERR(led->enable_gpio);
  267. dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret);
  268. return ret;
  269. }
  270. led->regulator = devm_regulator_get(&client->dev, "vled");
  271. if (IS_ERR(led->regulator))
  272. led->regulator = NULL;
  273. led->client = client;
  274. led->led_dev.name = led->label;
  275. led->led_dev.brightness_set_blocking = lm3692x_brightness_set;
  276. mutex_init(&led->lock);
  277. i2c_set_clientdata(client, led);
  278. led->regmap = devm_regmap_init_i2c(client, &lm3692x_regmap_config);
  279. if (IS_ERR(led->regmap)) {
  280. ret = PTR_ERR(led->regmap);
  281. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  282. ret);
  283. return ret;
  284. }
  285. ret = lm3692x_init(led);
  286. if (ret)
  287. return ret;
  288. ret = devm_led_classdev_register(&client->dev, &led->led_dev);
  289. if (ret) {
  290. dev_err(&client->dev, "led register err: %d\n", ret);
  291. return ret;
  292. }
  293. return 0;
  294. }
  295. static int lm3692x_remove(struct i2c_client *client)
  296. {
  297. struct lm3692x_led *led = i2c_get_clientdata(client);
  298. int ret;
  299. if (led->enable_gpio)
  300. gpiod_direction_output(led->enable_gpio, 0);
  301. if (led->regulator) {
  302. ret = regulator_disable(led->regulator);
  303. if (ret)
  304. dev_err(&led->client->dev,
  305. "Failed to disable regulator\n");
  306. }
  307. mutex_destroy(&led->lock);
  308. return 0;
  309. }
  310. static const struct i2c_device_id lm3692x_id[] = {
  311. { "lm36922", 0 },
  312. { "lm36923", 1 },
  313. { }
  314. };
  315. MODULE_DEVICE_TABLE(i2c, lm3692x_id);
  316. static const struct of_device_id of_lm3692x_leds_match[] = {
  317. { .compatible = "ti,lm36922", },
  318. { .compatible = "ti,lm36923", },
  319. {},
  320. };
  321. MODULE_DEVICE_TABLE(of, of_lm3692x_leds_match);
  322. static struct i2c_driver lm3692x_driver = {
  323. .driver = {
  324. .name = "lm3692x",
  325. .of_match_table = of_lm3692x_leds_match,
  326. },
  327. .probe = lm3692x_probe,
  328. .remove = lm3692x_remove,
  329. .id_table = lm3692x_id,
  330. };
  331. module_i2c_driver(lm3692x_driver);
  332. MODULE_DESCRIPTION("Texas Instruments LM3692X LED driver");
  333. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
  334. MODULE_LICENSE("GPL v2");