leds-lm3697.c 9.5 KB

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