leds-is31fl32xx.c 14 KB

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