leds-is31fl32xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for ISSI IS31FL32xx family of I2C LED controllers
  4. *
  5. * Copyright 2015 Allworx Corp.
  6. *
  7. * Datasheets:
  8. * http://www.issi.com/US/product-analog-fxled-driver.shtml
  9. * http://www.si-en.com/product.asp?parentid=890
  10. */
  11. #include <linux/device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/kernel.h>
  14. #include <linux/leds.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. /* Used to indicate a device has no such register */
  19. #define IS31FL32XX_REG_NONE 0xFF
  20. /* Software Shutdown bit in Shutdown Register */
  21. #define IS31FL32XX_SHUTDOWN_SSD_ENABLE 0
  22. #define IS31FL32XX_SHUTDOWN_SSD_DISABLE BIT(0)
  23. /* IS31FL3216 has a number of unique registers */
  24. #define IS31FL3216_CONFIG_REG 0x00
  25. #define IS31FL3216_LIGHTING_EFFECT_REG 0x03
  26. #define IS31FL3216_CHANNEL_CONFIG_REG 0x04
  27. /* Software Shutdown bit in 3216 Config Register */
  28. #define IS31FL3216_CONFIG_SSD_ENABLE BIT(7)
  29. #define IS31FL3216_CONFIG_SSD_DISABLE 0
  30. struct is31fl32xx_priv;
  31. struct is31fl32xx_led_data {
  32. struct led_classdev cdev;
  33. u8 channel; /* 1-based, max priv->cdef->channels */
  34. struct is31fl32xx_priv *priv;
  35. };
  36. struct is31fl32xx_priv {
  37. const struct is31fl32xx_chipdef *cdef;
  38. struct i2c_client *client;
  39. unsigned int num_leds;
  40. struct is31fl32xx_led_data leds[];
  41. };
  42. /**
  43. * struct is31fl32xx_chipdef - chip-specific attributes
  44. * @channels : Number of LED channels
  45. * @shutdown_reg : address of Shutdown register (optional)
  46. * @pwm_update_reg : address of PWM Update register
  47. * @global_control_reg : address of Global Control register (optional)
  48. * @reset_reg : address of Reset register (optional)
  49. * @pwm_register_base : address of first PWM register
  50. * @pwm_registers_reversed: : true if PWM registers count down instead of up
  51. * @led_control_register_base : address of first LED control register (optional)
  52. * @enable_bits_per_led_control_register: number of LEDs enable bits in each
  53. * @reset_func : pointer to reset function
  54. * @sw_shutdown_func : pointer to software shutdown function
  55. *
  56. * For all optional register addresses, the sentinel value %IS31FL32XX_REG_NONE
  57. * indicates that this chip has no such register.
  58. *
  59. * If non-NULL, @reset_func will be called during probing to set all
  60. * necessary registers to a known initialization state. This is needed
  61. * for chips that do not have a @reset_reg.
  62. *
  63. * @enable_bits_per_led_control_register must be >=1 if
  64. * @led_control_register_base != %IS31FL32XX_REG_NONE.
  65. */
  66. struct is31fl32xx_chipdef {
  67. u8 channels;
  68. u8 shutdown_reg;
  69. u8 pwm_update_reg;
  70. u8 global_control_reg;
  71. u8 reset_reg;
  72. u8 pwm_register_base;
  73. bool pwm_registers_reversed;
  74. u8 led_control_register_base;
  75. u8 enable_bits_per_led_control_register;
  76. int (*reset_func)(struct is31fl32xx_priv *priv);
  77. int (*sw_shutdown_func)(struct is31fl32xx_priv *priv, bool enable);
  78. };
  79. static const struct is31fl32xx_chipdef is31fl3236_cdef = {
  80. .channels = 36,
  81. .shutdown_reg = 0x00,
  82. .pwm_update_reg = 0x25,
  83. .global_control_reg = 0x4a,
  84. .reset_reg = 0x4f,
  85. .pwm_register_base = 0x01,
  86. .led_control_register_base = 0x26,
  87. .enable_bits_per_led_control_register = 1,
  88. };
  89. static const struct is31fl32xx_chipdef is31fl3235_cdef = {
  90. .channels = 28,
  91. .shutdown_reg = 0x00,
  92. .pwm_update_reg = 0x25,
  93. .global_control_reg = 0x4a,
  94. .reset_reg = 0x4f,
  95. .pwm_register_base = 0x05,
  96. .led_control_register_base = 0x2a,
  97. .enable_bits_per_led_control_register = 1,
  98. };
  99. static const struct is31fl32xx_chipdef is31fl3218_cdef = {
  100. .channels = 18,
  101. .shutdown_reg = 0x00,
  102. .pwm_update_reg = 0x16,
  103. .global_control_reg = IS31FL32XX_REG_NONE,
  104. .reset_reg = 0x17,
  105. .pwm_register_base = 0x01,
  106. .led_control_register_base = 0x13,
  107. .enable_bits_per_led_control_register = 6,
  108. };
  109. static int is31fl3216_reset(struct is31fl32xx_priv *priv);
  110. static int is31fl3216_software_shutdown(struct is31fl32xx_priv *priv,
  111. bool enable);
  112. static const struct is31fl32xx_chipdef is31fl3216_cdef = {
  113. .channels = 16,
  114. .shutdown_reg = IS31FL32XX_REG_NONE,
  115. .pwm_update_reg = 0xB0,
  116. .global_control_reg = IS31FL32XX_REG_NONE,
  117. .reset_reg = IS31FL32XX_REG_NONE,
  118. .pwm_register_base = 0x10,
  119. .pwm_registers_reversed = true,
  120. .led_control_register_base = 0x01,
  121. .enable_bits_per_led_control_register = 8,
  122. .reset_func = is31fl3216_reset,
  123. .sw_shutdown_func = is31fl3216_software_shutdown,
  124. };
  125. static int is31fl32xx_write(struct is31fl32xx_priv *priv, u8 reg, u8 val)
  126. {
  127. int ret;
  128. dev_dbg(&priv->client->dev, "writing register 0x%02X=0x%02X", reg, val);
  129. ret = i2c_smbus_write_byte_data(priv->client, reg, val);
  130. if (ret) {
  131. dev_err(&priv->client->dev,
  132. "register write to 0x%02X failed (error %d)",
  133. reg, ret);
  134. }
  135. return ret;
  136. }
  137. /*
  138. * Custom reset function for IS31FL3216 because it does not have a RESET
  139. * register the way that the other IS31FL32xx chips do. We don't bother
  140. * writing the GPIO and animation registers, because the registers we
  141. * do write ensure those will have no effect.
  142. */
  143. static int is31fl3216_reset(struct is31fl32xx_priv *priv)
  144. {
  145. unsigned int i;
  146. int ret;
  147. ret = is31fl32xx_write(priv, IS31FL3216_CONFIG_REG,
  148. IS31FL3216_CONFIG_SSD_ENABLE);
  149. if (ret)
  150. return ret;
  151. for (i = 0; i < priv->cdef->channels; i++) {
  152. ret = is31fl32xx_write(priv, priv->cdef->pwm_register_base+i,
  153. 0x00);
  154. if (ret)
  155. return ret;
  156. }
  157. ret = is31fl32xx_write(priv, priv->cdef->pwm_update_reg, 0);
  158. if (ret)
  159. return ret;
  160. ret = is31fl32xx_write(priv, IS31FL3216_LIGHTING_EFFECT_REG, 0x00);
  161. if (ret)
  162. return ret;
  163. ret = is31fl32xx_write(priv, IS31FL3216_CHANNEL_CONFIG_REG, 0x00);
  164. if (ret)
  165. return ret;
  166. return 0;
  167. }
  168. /*
  169. * Custom Software-Shutdown function for IS31FL3216 because it does not have
  170. * a SHUTDOWN register the way that the other IS31FL32xx chips do.
  171. * We don't bother doing a read/modify/write on the CONFIG register because
  172. * we only ever use a value of '0' for the other fields in that register.
  173. */
  174. static int is31fl3216_software_shutdown(struct is31fl32xx_priv *priv,
  175. bool enable)
  176. {
  177. u8 value = enable ? IS31FL3216_CONFIG_SSD_ENABLE :
  178. IS31FL3216_CONFIG_SSD_DISABLE;
  179. return is31fl32xx_write(priv, IS31FL3216_CONFIG_REG, value);
  180. }
  181. /*
  182. * NOTE: A mutex is not needed in this function because:
  183. * - All referenced data is read-only after probe()
  184. * - The I2C core has a mutex on to protect the bus
  185. * - There are no read/modify/write operations
  186. * - Intervening operations between the write of the PWM register
  187. * and the Update register are harmless.
  188. *
  189. * Example:
  190. * PWM_REG_1 write 16
  191. * UPDATE_REG write 0
  192. * PWM_REG_2 write 128
  193. * UPDATE_REG write 0
  194. * vs:
  195. * PWM_REG_1 write 16
  196. * PWM_REG_2 write 128
  197. * UPDATE_REG write 0
  198. * UPDATE_REG write 0
  199. * are equivalent. Poking the Update register merely applies all PWM
  200. * register writes up to that point.
  201. */
  202. static int is31fl32xx_brightness_set(struct led_classdev *led_cdev,
  203. enum led_brightness brightness)
  204. {
  205. const struct is31fl32xx_led_data *led_data =
  206. container_of(led_cdev, struct is31fl32xx_led_data, cdev);
  207. const struct is31fl32xx_chipdef *cdef = led_data->priv->cdef;
  208. u8 pwm_register_offset;
  209. int ret;
  210. dev_dbg(led_cdev->dev, "%s: %d\n", __func__, brightness);
  211. /* NOTE: led_data->channel is 1-based */
  212. if (cdef->pwm_registers_reversed)
  213. pwm_register_offset = cdef->channels - led_data->channel;
  214. else
  215. pwm_register_offset = led_data->channel - 1;
  216. ret = is31fl32xx_write(led_data->priv,
  217. cdef->pwm_register_base + pwm_register_offset,
  218. brightness);
  219. if (ret)
  220. return ret;
  221. return is31fl32xx_write(led_data->priv, cdef->pwm_update_reg, 0);
  222. }
  223. static int is31fl32xx_reset_regs(struct is31fl32xx_priv *priv)
  224. {
  225. const struct is31fl32xx_chipdef *cdef = priv->cdef;
  226. int ret;
  227. if (cdef->reset_reg != IS31FL32XX_REG_NONE) {
  228. ret = is31fl32xx_write(priv, cdef->reset_reg, 0);
  229. if (ret)
  230. return ret;
  231. }
  232. if (cdef->reset_func)
  233. return cdef->reset_func(priv);
  234. return 0;
  235. }
  236. static int is31fl32xx_software_shutdown(struct is31fl32xx_priv *priv,
  237. bool enable)
  238. {
  239. const struct is31fl32xx_chipdef *cdef = priv->cdef;
  240. int ret;
  241. if (cdef->shutdown_reg != IS31FL32XX_REG_NONE) {
  242. u8 value = enable ? IS31FL32XX_SHUTDOWN_SSD_ENABLE :
  243. IS31FL32XX_SHUTDOWN_SSD_DISABLE;
  244. ret = is31fl32xx_write(priv, cdef->shutdown_reg, value);
  245. if (ret)
  246. return ret;
  247. }
  248. if (cdef->sw_shutdown_func)
  249. return cdef->sw_shutdown_func(priv, enable);
  250. return 0;
  251. }
  252. static int is31fl32xx_init_regs(struct is31fl32xx_priv *priv)
  253. {
  254. const struct is31fl32xx_chipdef *cdef = priv->cdef;
  255. int ret;
  256. ret = is31fl32xx_reset_regs(priv);
  257. if (ret)
  258. return ret;
  259. /*
  260. * Set enable bit for all channels.
  261. * We will control state with PWM registers alone.
  262. */
  263. if (cdef->led_control_register_base != IS31FL32XX_REG_NONE) {
  264. u8 value =
  265. GENMASK(cdef->enable_bits_per_led_control_register-1, 0);
  266. u8 num_regs = cdef->channels /
  267. cdef->enable_bits_per_led_control_register;
  268. int i;
  269. for (i = 0; i < num_regs; i++) {
  270. ret = is31fl32xx_write(priv,
  271. cdef->led_control_register_base+i,
  272. value);
  273. if (ret)
  274. return ret;
  275. }
  276. }
  277. ret = is31fl32xx_software_shutdown(priv, false);
  278. if (ret)
  279. return ret;
  280. if (cdef->global_control_reg != IS31FL32XX_REG_NONE) {
  281. ret = is31fl32xx_write(priv, cdef->global_control_reg, 0x00);
  282. if (ret)
  283. return ret;
  284. }
  285. return 0;
  286. }
  287. static int is31fl32xx_parse_child_dt(const struct device *dev,
  288. const struct device_node *child,
  289. struct is31fl32xx_led_data *led_data)
  290. {
  291. struct led_classdev *cdev = &led_data->cdev;
  292. int ret = 0;
  293. u32 reg;
  294. ret = of_property_read_u32(child, "reg", &reg);
  295. if (ret || reg < 1 || reg > led_data->priv->cdef->channels) {
  296. dev_err(dev,
  297. "Child node %pOF does not have a valid reg property\n",
  298. child);
  299. return -EINVAL;
  300. }
  301. led_data->channel = reg;
  302. cdev->brightness_set_blocking = is31fl32xx_brightness_set;
  303. return 0;
  304. }
  305. static struct is31fl32xx_led_data *is31fl32xx_find_led_data(
  306. struct is31fl32xx_priv *priv,
  307. u8 channel)
  308. {
  309. size_t i;
  310. for (i = 0; i < priv->num_leds; i++) {
  311. if (priv->leds[i].channel == channel)
  312. return &priv->leds[i];
  313. }
  314. return NULL;
  315. }
  316. static int is31fl32xx_parse_dt(struct device *dev,
  317. struct is31fl32xx_priv *priv)
  318. {
  319. struct device_node *child;
  320. int ret = 0;
  321. for_each_available_child_of_node(dev_of_node(dev), child) {
  322. struct led_init_data init_data = {};
  323. struct is31fl32xx_led_data *led_data =
  324. &priv->leds[priv->num_leds];
  325. const struct is31fl32xx_led_data *other_led_data;
  326. led_data->priv = priv;
  327. ret = is31fl32xx_parse_child_dt(dev, child, led_data);
  328. if (ret)
  329. goto err;
  330. /* Detect if channel is already in use by another child */
  331. other_led_data = is31fl32xx_find_led_data(priv,
  332. led_data->channel);
  333. if (other_led_data) {
  334. dev_err(dev,
  335. "Node %pOF 'reg' conflicts with another LED\n",
  336. child);
  337. ret = -EINVAL;
  338. goto err;
  339. }
  340. init_data.fwnode = of_fwnode_handle(child);
  341. ret = devm_led_classdev_register_ext(dev, &led_data->cdev,
  342. &init_data);
  343. if (ret) {
  344. dev_err(dev, "Failed to register LED for %pOF: %d\n",
  345. child, ret);
  346. goto err;
  347. }
  348. priv->num_leds++;
  349. }
  350. return 0;
  351. err:
  352. of_node_put(child);
  353. return ret;
  354. }
  355. static const struct of_device_id of_is31fl32xx_match[] = {
  356. { .compatible = "issi,is31fl3236", .data = &is31fl3236_cdef, },
  357. { .compatible = "issi,is31fl3235", .data = &is31fl3235_cdef, },
  358. { .compatible = "issi,is31fl3218", .data = &is31fl3218_cdef, },
  359. { .compatible = "si-en,sn3218", .data = &is31fl3218_cdef, },
  360. { .compatible = "issi,is31fl3216", .data = &is31fl3216_cdef, },
  361. { .compatible = "si-en,sn3216", .data = &is31fl3216_cdef, },
  362. {},
  363. };
  364. MODULE_DEVICE_TABLE(of, of_is31fl32xx_match);
  365. static int is31fl32xx_probe(struct i2c_client *client,
  366. const struct i2c_device_id *id)
  367. {
  368. const struct is31fl32xx_chipdef *cdef;
  369. struct device *dev = &client->dev;
  370. struct is31fl32xx_priv *priv;
  371. int count;
  372. int ret = 0;
  373. cdef = device_get_match_data(dev);
  374. count = of_get_available_child_count(dev_of_node(dev));
  375. if (!count)
  376. return -EINVAL;
  377. priv = devm_kzalloc(dev, struct_size(priv, leds, count),
  378. GFP_KERNEL);
  379. if (!priv)
  380. return -ENOMEM;
  381. priv->client = client;
  382. priv->cdef = cdef;
  383. i2c_set_clientdata(client, priv);
  384. ret = is31fl32xx_init_regs(priv);
  385. if (ret)
  386. return ret;
  387. ret = is31fl32xx_parse_dt(dev, priv);
  388. if (ret)
  389. return ret;
  390. return 0;
  391. }
  392. static void is31fl32xx_remove(struct i2c_client *client)
  393. {
  394. struct is31fl32xx_priv *priv = i2c_get_clientdata(client);
  395. int ret;
  396. ret = is31fl32xx_reset_regs(priv);
  397. if (ret)
  398. dev_err(&client->dev, "Failed to reset registers on removal (%pe)\n",
  399. ERR_PTR(ret));
  400. }
  401. /*
  402. * i2c-core (and modalias) requires that id_table be properly filled,
  403. * even though it is not used for DeviceTree based instantiation.
  404. */
  405. static const struct i2c_device_id is31fl32xx_id[] = {
  406. { "is31fl3236" },
  407. { "is31fl3235" },
  408. { "is31fl3218" },
  409. { "sn3218" },
  410. { "is31fl3216" },
  411. { "sn3216" },
  412. {},
  413. };
  414. MODULE_DEVICE_TABLE(i2c, is31fl32xx_id);
  415. static struct i2c_driver is31fl32xx_driver = {
  416. .driver = {
  417. .name = "is31fl32xx",
  418. .of_match_table = of_is31fl32xx_match,
  419. },
  420. .probe = is31fl32xx_probe,
  421. .remove = is31fl32xx_remove,
  422. .id_table = is31fl32xx_id,
  423. };
  424. module_i2c_driver(is31fl32xx_driver);
  425. MODULE_AUTHOR("David Rivshin <[email protected]>");
  426. MODULE_DESCRIPTION("ISSI IS31FL32xx LED driver");
  427. MODULE_LICENSE("GPL v2");