leds-lm3697.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0
  2. // TI LM3697 LED chip family driver
  3. // Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
  4. #include <linux/gpio/consumer.h>
  5. #include <linux/i2c.h>
  6. #include <linux/of.h>
  7. #include <linux/of_gpio.h>
  8. #include <linux/regulator/consumer.h>
  9. #include <linux/leds-ti-lmu-common.h>
  10. #define LM3697_REV 0x0
  11. #define LM3697_RESET 0x1
  12. #define LM3697_OUTPUT_CONFIG 0x10
  13. #define LM3697_CTRL_A_RAMP 0x11
  14. #define LM3697_CTRL_B_RAMP 0x12
  15. #define LM3697_CTRL_A_B_RT_RAMP 0x13
  16. #define LM3697_CTRL_A_B_RAMP_CFG 0x14
  17. #define LM3697_CTRL_A_B_BRT_CFG 0x16
  18. #define LM3697_CTRL_A_FS_CURR_CFG 0x17
  19. #define LM3697_CTRL_B_FS_CURR_CFG 0x18
  20. #define LM3697_PWM_CFG 0x1c
  21. #define LM3697_CTRL_A_BRT_LSB 0x20
  22. #define LM3697_CTRL_A_BRT_MSB 0x21
  23. #define LM3697_CTRL_B_BRT_LSB 0x22
  24. #define LM3697_CTRL_B_BRT_MSB 0x23
  25. #define LM3697_CTRL_ENABLE 0x24
  26. #define LM3697_SW_RESET BIT(0)
  27. #define LM3697_CTRL_A_EN BIT(0)
  28. #define LM3697_CTRL_B_EN BIT(1)
  29. #define LM3697_CTRL_A_B_EN (LM3697_CTRL_A_EN | LM3697_CTRL_B_EN)
  30. #define LM3697_MAX_LED_STRINGS 3
  31. #define LM3697_CONTROL_A 0
  32. #define LM3697_CONTROL_B 1
  33. #define LM3697_MAX_CONTROL_BANKS 2
  34. /**
  35. * struct lm3697_led -
  36. * @hvled_strings: Array of LED strings associated with a control bank
  37. * @label: LED label
  38. * @led_dev: LED class device
  39. * @priv: Pointer to the device struct
  40. * @lmu_data: Register and setting values for common code
  41. * @control_bank: Control bank the LED is associated to. 0 is control bank A
  42. * 1 is control bank B
  43. */
  44. struct lm3697_led {
  45. u32 hvled_strings[LM3697_MAX_LED_STRINGS];
  46. char label[LED_MAX_NAME_SIZE];
  47. struct led_classdev led_dev;
  48. struct lm3697 *priv;
  49. struct ti_lmu_bank lmu_data;
  50. int control_bank;
  51. int enabled;
  52. int num_leds;
  53. };
  54. /**
  55. * struct lm3697 -
  56. * @enable_gpio: Hardware enable gpio
  57. * @regulator: LED supply regulator pointer
  58. * @client: Pointer to the I2C client
  59. * @regmap: Devices register map
  60. * @dev: Pointer to the devices device struct
  61. * @lock: Lock for reading/writing the device
  62. * @leds: Array of LED strings
  63. */
  64. struct lm3697 {
  65. struct gpio_desc *enable_gpio;
  66. struct regulator *regulator;
  67. struct i2c_client *client;
  68. struct regmap *regmap;
  69. struct device *dev;
  70. struct mutex lock;
  71. int bank_cfg;
  72. int num_banks;
  73. struct lm3697_led leds[];
  74. };
  75. static const struct reg_default lm3697_reg_defs[] = {
  76. {LM3697_OUTPUT_CONFIG, 0x6},
  77. {LM3697_CTRL_A_RAMP, 0x0},
  78. {LM3697_CTRL_B_RAMP, 0x0},
  79. {LM3697_CTRL_A_B_RT_RAMP, 0x0},
  80. {LM3697_CTRL_A_B_RAMP_CFG, 0x0},
  81. {LM3697_CTRL_A_B_BRT_CFG, 0x0},
  82. {LM3697_CTRL_A_FS_CURR_CFG, 0x13},
  83. {LM3697_CTRL_B_FS_CURR_CFG, 0x13},
  84. {LM3697_PWM_CFG, 0xc},
  85. {LM3697_CTRL_A_BRT_LSB, 0x0},
  86. {LM3697_CTRL_A_BRT_MSB, 0x0},
  87. {LM3697_CTRL_B_BRT_LSB, 0x0},
  88. {LM3697_CTRL_B_BRT_MSB, 0x0},
  89. {LM3697_CTRL_ENABLE, 0x0},
  90. };
  91. static const struct regmap_config lm3697_regmap_config = {
  92. .reg_bits = 8,
  93. .val_bits = 8,
  94. .max_register = LM3697_CTRL_ENABLE,
  95. .reg_defaults = lm3697_reg_defs,
  96. .num_reg_defaults = ARRAY_SIZE(lm3697_reg_defs),
  97. .cache_type = REGCACHE_FLAT,
  98. };
  99. static int lm3697_brightness_set(struct led_classdev *led_cdev,
  100. enum led_brightness brt_val)
  101. {
  102. struct lm3697_led *led = container_of(led_cdev, struct lm3697_led,
  103. led_dev);
  104. int ctrl_en_val = (1 << led->control_bank);
  105. struct device *dev = led->priv->dev;
  106. int ret;
  107. mutex_lock(&led->priv->lock);
  108. if (brt_val == LED_OFF) {
  109. ret = regmap_update_bits(led->priv->regmap, LM3697_CTRL_ENABLE,
  110. ctrl_en_val, ~ctrl_en_val);
  111. if (ret) {
  112. dev_err(dev, "Cannot write ctrl register\n");
  113. goto brightness_out;
  114. }
  115. led->enabled = LED_OFF;
  116. } else {
  117. ret = ti_lmu_common_set_brightness(&led->lmu_data, brt_val);
  118. if (ret) {
  119. dev_err(dev, "Cannot write brightness\n");
  120. goto brightness_out;
  121. }
  122. if (!led->enabled) {
  123. ret = regmap_update_bits(led->priv->regmap,
  124. LM3697_CTRL_ENABLE,
  125. ctrl_en_val, ctrl_en_val);
  126. if (ret) {
  127. dev_err(dev, "Cannot enable the device\n");
  128. goto brightness_out;
  129. }
  130. led->enabled = brt_val;
  131. }
  132. }
  133. brightness_out:
  134. mutex_unlock(&led->priv->lock);
  135. return ret;
  136. }
  137. static int lm3697_init(struct lm3697 *priv)
  138. {
  139. struct device *dev = priv->dev;
  140. struct lm3697_led *led;
  141. int i, ret;
  142. if (priv->enable_gpio) {
  143. gpiod_direction_output(priv->enable_gpio, 1);
  144. } else {
  145. ret = regmap_write(priv->regmap, LM3697_RESET, LM3697_SW_RESET);
  146. if (ret) {
  147. dev_err(dev, "Cannot reset the device\n");
  148. goto out;
  149. }
  150. }
  151. ret = regmap_write(priv->regmap, LM3697_CTRL_ENABLE, 0x0);
  152. if (ret) {
  153. dev_err(dev, "Cannot write ctrl enable\n");
  154. goto out;
  155. }
  156. ret = regmap_write(priv->regmap, LM3697_OUTPUT_CONFIG, priv->bank_cfg);
  157. if (ret)
  158. dev_err(dev, "Cannot write OUTPUT config\n");
  159. for (i = 0; i < priv->num_banks; i++) {
  160. led = &priv->leds[i];
  161. ret = ti_lmu_common_set_ramp(&led->lmu_data);
  162. if (ret)
  163. dev_err(dev, "Setting the ramp rate failed\n");
  164. }
  165. out:
  166. return ret;
  167. }
  168. static int lm3697_probe_dt(struct lm3697 *priv)
  169. {
  170. struct fwnode_handle *child = NULL;
  171. struct device *dev = priv->dev;
  172. struct lm3697_led *led;
  173. int ret = -EINVAL;
  174. int control_bank;
  175. size_t i = 0;
  176. int j;
  177. priv->enable_gpio = devm_gpiod_get_optional(dev, "enable",
  178. GPIOD_OUT_LOW);
  179. if (IS_ERR(priv->enable_gpio))
  180. return dev_err_probe(dev, PTR_ERR(priv->enable_gpio),
  181. "Failed to get enable GPIO\n");
  182. priv->regulator = devm_regulator_get(dev, "vled");
  183. if (IS_ERR(priv->regulator))
  184. priv->regulator = NULL;
  185. device_for_each_child_node(dev, child) {
  186. struct led_init_data init_data = {};
  187. ret = fwnode_property_read_u32(child, "reg", &control_bank);
  188. if (ret) {
  189. dev_err(dev, "reg property missing\n");
  190. fwnode_handle_put(child);
  191. goto child_out;
  192. }
  193. if (control_bank > LM3697_CONTROL_B) {
  194. dev_err(dev, "reg property is invalid\n");
  195. ret = -EINVAL;
  196. fwnode_handle_put(child);
  197. goto child_out;
  198. }
  199. led = &priv->leds[i];
  200. ret = ti_lmu_common_get_brt_res(dev, child, &led->lmu_data);
  201. if (ret)
  202. dev_warn(dev,
  203. "brightness resolution property missing\n");
  204. led->control_bank = control_bank;
  205. led->lmu_data.regmap = priv->regmap;
  206. led->lmu_data.runtime_ramp_reg = LM3697_CTRL_A_RAMP +
  207. control_bank;
  208. led->lmu_data.msb_brightness_reg = LM3697_CTRL_A_BRT_MSB +
  209. led->control_bank * 2;
  210. led->lmu_data.lsb_brightness_reg = LM3697_CTRL_A_BRT_LSB +
  211. led->control_bank * 2;
  212. led->num_leds = fwnode_property_count_u32(child, "led-sources");
  213. if (led->num_leds > LM3697_MAX_LED_STRINGS) {
  214. dev_err(dev, "Too many LED strings defined\n");
  215. continue;
  216. }
  217. ret = fwnode_property_read_u32_array(child, "led-sources",
  218. led->hvled_strings,
  219. led->num_leds);
  220. if (ret) {
  221. dev_err(dev, "led-sources property missing\n");
  222. fwnode_handle_put(child);
  223. goto child_out;
  224. }
  225. for (j = 0; j < led->num_leds; j++)
  226. priv->bank_cfg |=
  227. (led->control_bank << led->hvled_strings[j]);
  228. ret = ti_lmu_common_get_ramp_params(dev, child, &led->lmu_data);
  229. if (ret)
  230. dev_warn(dev, "runtime-ramp properties missing\n");
  231. init_data.fwnode = child;
  232. init_data.devicename = priv->client->name;
  233. /* for backwards compatibility if `label` is not present */
  234. init_data.default_label = ":";
  235. led->priv = priv;
  236. led->led_dev.max_brightness = led->lmu_data.max_brightness;
  237. led->led_dev.brightness_set_blocking = lm3697_brightness_set;
  238. ret = devm_led_classdev_register_ext(dev, &led->led_dev,
  239. &init_data);
  240. if (ret) {
  241. dev_err(dev, "led register err: %d\n", ret);
  242. fwnode_handle_put(child);
  243. goto child_out;
  244. }
  245. i++;
  246. }
  247. child_out:
  248. return ret;
  249. }
  250. static int lm3697_probe(struct i2c_client *client,
  251. const struct i2c_device_id *id)
  252. {
  253. struct device *dev = &client->dev;
  254. struct lm3697 *led;
  255. int count;
  256. int ret;
  257. count = device_get_child_node_count(dev);
  258. if (!count || count > LM3697_MAX_CONTROL_BANKS) {
  259. dev_err(dev, "Strange device tree!");
  260. return -ENODEV;
  261. }
  262. led = devm_kzalloc(dev, struct_size(led, leds, count), GFP_KERNEL);
  263. if (!led)
  264. return -ENOMEM;
  265. mutex_init(&led->lock);
  266. i2c_set_clientdata(client, led);
  267. led->client = client;
  268. led->dev = dev;
  269. led->num_banks = count;
  270. led->regmap = devm_regmap_init_i2c(client, &lm3697_regmap_config);
  271. if (IS_ERR(led->regmap)) {
  272. ret = PTR_ERR(led->regmap);
  273. dev_err(dev, "Failed to allocate register map: %d\n", ret);
  274. return ret;
  275. }
  276. ret = lm3697_probe_dt(led);
  277. if (ret)
  278. return ret;
  279. return lm3697_init(led);
  280. }
  281. static int lm3697_remove(struct i2c_client *client)
  282. {
  283. struct lm3697 *led = i2c_get_clientdata(client);
  284. struct device *dev = &led->client->dev;
  285. int ret;
  286. ret = regmap_update_bits(led->regmap, LM3697_CTRL_ENABLE,
  287. LM3697_CTRL_A_B_EN, 0);
  288. if (ret) {
  289. dev_err(dev, "Failed to disable the device\n");
  290. return ret;
  291. }
  292. if (led->enable_gpio)
  293. gpiod_direction_output(led->enable_gpio, 0);
  294. if (led->regulator) {
  295. ret = regulator_disable(led->regulator);
  296. if (ret)
  297. dev_err(dev, "Failed to disable regulator\n");
  298. }
  299. mutex_destroy(&led->lock);
  300. return 0;
  301. }
  302. static const struct i2c_device_id lm3697_id[] = {
  303. { "lm3697", 0 },
  304. { }
  305. };
  306. MODULE_DEVICE_TABLE(i2c, lm3697_id);
  307. static const struct of_device_id of_lm3697_leds_match[] = {
  308. { .compatible = "ti,lm3697", },
  309. {},
  310. };
  311. MODULE_DEVICE_TABLE(of, of_lm3697_leds_match);
  312. static struct i2c_driver lm3697_driver = {
  313. .driver = {
  314. .name = "lm3697",
  315. .of_match_table = of_lm3697_leds_match,
  316. },
  317. .probe = lm3697_probe,
  318. .remove = lm3697_remove,
  319. .id_table = lm3697_id,
  320. };
  321. module_i2c_driver(lm3697_driver);
  322. MODULE_DESCRIPTION("Texas Instruments LM3697 LED driver");
  323. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
  324. MODULE_LICENSE("GPL v2");