leds-is31fl319x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2015-16 Golden Delicious Computers
  4. *
  5. * Author: Nikolaus Schaller <hns@goldelico.com>
  6. *
  7. * LED driver for the IS31FL319{0,1,3,6,9} to drive 1, 3, 6 or 9 light
  8. * effect LEDs.
  9. */
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/leds.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio/consumer.h>
  20. /* register numbers */
  21. #define IS31FL319X_SHUTDOWN 0x00
  22. #define IS31FL319X_CTRL1 0x01
  23. #define IS31FL319X_CTRL2 0x02
  24. #define IS31FL319X_CONFIG1 0x03
  25. #define IS31FL319X_CONFIG2 0x04
  26. #define IS31FL319X_RAMP_MODE 0x05
  27. #define IS31FL319X_BREATH_MASK 0x06
  28. #define IS31FL319X_PWM(channel) (0x07 + channel)
  29. #define IS31FL319X_DATA_UPDATE 0x10
  30. #define IS31FL319X_T0(channel) (0x11 + channel)
  31. #define IS31FL319X_T123_1 0x1a
  32. #define IS31FL319X_T123_2 0x1b
  33. #define IS31FL319X_T123_3 0x1c
  34. #define IS31FL319X_T4(channel) (0x1d + channel)
  35. #define IS31FL319X_TIME_UPDATE 0x26
  36. #define IS31FL319X_RESET 0xff
  37. #define IS31FL319X_REG_CNT (IS31FL319X_RESET + 1)
  38. #define IS31FL319X_MAX_LEDS 9
  39. /* CS (Current Setting) in CONFIG2 register */
  40. #define IS31FL319X_CONFIG2_CS_SHIFT 4
  41. #define IS31FL319X_CONFIG2_CS_MASK 0x7
  42. #define IS31FL319X_CONFIG2_CS_STEP_REF 12
  43. #define IS31FL319X_CURRENT_MIN ((u32)5000)
  44. #define IS31FL319X_CURRENT_MAX ((u32)40000)
  45. #define IS31FL319X_CURRENT_STEP ((u32)5000)
  46. #define IS31FL319X_CURRENT_DEFAULT ((u32)20000)
  47. /* Audio gain in CONFIG2 register */
  48. #define IS31FL319X_AUDIO_GAIN_DB_MAX ((u32)21)
  49. #define IS31FL319X_AUDIO_GAIN_DB_STEP ((u32)3)
  50. /*
  51. * regmap is used as a cache of chip's register space,
  52. * to avoid reading back brightness values from chip,
  53. * which is known to hang.
  54. */
  55. struct is31fl319x_chip {
  56. const struct is31fl319x_chipdef *cdef;
  57. struct i2c_client *client;
  58. struct gpio_desc *shutdown_gpio;
  59. struct regmap *regmap;
  60. struct mutex lock;
  61. u32 audio_gain_db;
  62. struct is31fl319x_led {
  63. struct is31fl319x_chip *chip;
  64. struct led_classdev cdev;
  65. u32 max_microamp;
  66. bool configured;
  67. } leds[IS31FL319X_MAX_LEDS];
  68. };
  69. struct is31fl319x_chipdef {
  70. int num_leds;
  71. };
  72. static const struct is31fl319x_chipdef is31fl3190_cdef = {
  73. .num_leds = 1,
  74. };
  75. static const struct is31fl319x_chipdef is31fl3193_cdef = {
  76. .num_leds = 3,
  77. };
  78. static const struct is31fl319x_chipdef is31fl3196_cdef = {
  79. .num_leds = 6,
  80. };
  81. static const struct is31fl319x_chipdef is31fl3199_cdef = {
  82. .num_leds = 9,
  83. };
  84. static const struct of_device_id of_is31fl319x_match[] = {
  85. { .compatible = "issi,is31fl3190", .data = &is31fl3190_cdef, },
  86. { .compatible = "issi,is31fl3191", .data = &is31fl3190_cdef, },
  87. { .compatible = "issi,is31fl3193", .data = &is31fl3193_cdef, },
  88. { .compatible = "issi,is31fl3196", .data = &is31fl3196_cdef, },
  89. { .compatible = "issi,is31fl3199", .data = &is31fl3199_cdef, },
  90. { .compatible = "si-en,sn3199", .data = &is31fl3199_cdef, },
  91. { }
  92. };
  93. MODULE_DEVICE_TABLE(of, of_is31fl319x_match);
  94. static int is31fl319x_brightness_set(struct led_classdev *cdev,
  95. enum led_brightness brightness)
  96. {
  97. struct is31fl319x_led *led = container_of(cdev, struct is31fl319x_led,
  98. cdev);
  99. struct is31fl319x_chip *is31 = led->chip;
  100. int chan = led - is31->leds;
  101. int ret;
  102. int i;
  103. u8 ctrl1 = 0, ctrl2 = 0;
  104. dev_dbg(&is31->client->dev, "%s %d: %d\n", __func__, chan, brightness);
  105. mutex_lock(&is31->lock);
  106. /* update PWM register */
  107. ret = regmap_write(is31->regmap, IS31FL319X_PWM(chan), brightness);
  108. if (ret < 0)
  109. goto out;
  110. /* read current brightness of all PWM channels */
  111. for (i = 0; i < is31->cdef->num_leds; i++) {
  112. unsigned int pwm_value;
  113. bool on;
  114. /*
  115. * since neither cdev nor the chip can provide
  116. * the current setting, we read from the regmap cache
  117. */
  118. ret = regmap_read(is31->regmap, IS31FL319X_PWM(i), &pwm_value);
  119. dev_dbg(&is31->client->dev, "%s read %d: ret=%d: %d\n",
  120. __func__, i, ret, pwm_value);
  121. on = ret >= 0 && pwm_value > LED_OFF;
  122. if (i < 3)
  123. ctrl1 |= on << i; /* 0..2 => bit 0..2 */
  124. else if (i < 6)
  125. ctrl1 |= on << (i + 1); /* 3..5 => bit 4..6 */
  126. else
  127. ctrl2 |= on << (i - 6); /* 6..8 => bit 0..2 */
  128. }
  129. if (ctrl1 > 0 || ctrl2 > 0) {
  130. dev_dbg(&is31->client->dev, "power up %02x %02x\n",
  131. ctrl1, ctrl2);
  132. regmap_write(is31->regmap, IS31FL319X_CTRL1, ctrl1);
  133. regmap_write(is31->regmap, IS31FL319X_CTRL2, ctrl2);
  134. /* update PWMs */
  135. regmap_write(is31->regmap, IS31FL319X_DATA_UPDATE, 0x00);
  136. /* enable chip from shut down */
  137. ret = regmap_write(is31->regmap, IS31FL319X_SHUTDOWN, 0x01);
  138. } else {
  139. dev_dbg(&is31->client->dev, "power down\n");
  140. /* shut down (no need to clear CTRL1/2) */
  141. ret = regmap_write(is31->regmap, IS31FL319X_SHUTDOWN, 0x00);
  142. }
  143. out:
  144. mutex_unlock(&is31->lock);
  145. return ret;
  146. }
  147. static int is31fl319x_parse_child_dt(const struct device *dev,
  148. const struct device_node *child,
  149. struct is31fl319x_led *led)
  150. {
  151. struct led_classdev *cdev = &led->cdev;
  152. int ret;
  153. if (of_property_read_string(child, "label", &cdev->name))
  154. cdev->name = child->name;
  155. ret = of_property_read_string(child, "linux,default-trigger",
  156. &cdev->default_trigger);
  157. if (ret < 0 && ret != -EINVAL) /* is optional */
  158. return ret;
  159. led->max_microamp = IS31FL319X_CURRENT_DEFAULT;
  160. ret = of_property_read_u32(child, "led-max-microamp",
  161. &led->max_microamp);
  162. if (!ret) {
  163. if (led->max_microamp < IS31FL319X_CURRENT_MIN)
  164. return -EINVAL; /* not supported */
  165. led->max_microamp = min(led->max_microamp,
  166. IS31FL319X_CURRENT_MAX);
  167. }
  168. return 0;
  169. }
  170. static int is31fl319x_parse_dt(struct device *dev,
  171. struct is31fl319x_chip *is31)
  172. {
  173. struct device_node *np = dev_of_node(dev), *child;
  174. int count;
  175. int ret;
  176. if (!np)
  177. return -ENODEV;
  178. is31->shutdown_gpio = devm_gpiod_get_optional(dev,
  179. "shutdown",
  180. GPIOD_OUT_HIGH);
  181. if (IS_ERR(is31->shutdown_gpio)) {
  182. ret = PTR_ERR(is31->shutdown_gpio);
  183. dev_err(dev, "Failed to get shutdown gpio: %d\n", ret);
  184. return ret;
  185. }
  186. is31->cdef = device_get_match_data(dev);
  187. count = of_get_available_child_count(np);
  188. dev_dbg(dev, "probing with %d leds defined in DT\n", count);
  189. if (!count || count > is31->cdef->num_leds) {
  190. dev_err(dev, "Number of leds defined must be between 1 and %u\n",
  191. is31->cdef->num_leds);
  192. return -ENODEV;
  193. }
  194. for_each_available_child_of_node(np, child) {
  195. struct is31fl319x_led *led;
  196. u32 reg;
  197. ret = of_property_read_u32(child, "reg", &reg);
  198. if (ret) {
  199. dev_err(dev, "Failed to read led 'reg' property\n");
  200. goto put_child_node;
  201. }
  202. if (reg < 1 || reg > is31->cdef->num_leds) {
  203. dev_err(dev, "invalid led reg %u\n", reg);
  204. ret = -EINVAL;
  205. goto put_child_node;
  206. }
  207. led = &is31->leds[reg - 1];
  208. if (led->configured) {
  209. dev_err(dev, "led %u is already configured\n", reg);
  210. ret = -EINVAL;
  211. goto put_child_node;
  212. }
  213. ret = is31fl319x_parse_child_dt(dev, child, led);
  214. if (ret) {
  215. dev_err(dev, "led %u DT parsing failed\n", reg);
  216. goto put_child_node;
  217. }
  218. led->configured = true;
  219. }
  220. is31->audio_gain_db = 0;
  221. ret = of_property_read_u32(np, "audio-gain-db", &is31->audio_gain_db);
  222. if (!ret)
  223. is31->audio_gain_db = min(is31->audio_gain_db,
  224. IS31FL319X_AUDIO_GAIN_DB_MAX);
  225. return 0;
  226. put_child_node:
  227. of_node_put(child);
  228. return ret;
  229. }
  230. static bool is31fl319x_readable_reg(struct device *dev, unsigned int reg)
  231. { /* we have no readable registers */
  232. return false;
  233. }
  234. static bool is31fl319x_volatile_reg(struct device *dev, unsigned int reg)
  235. { /* volatile registers are not cached */
  236. switch (reg) {
  237. case IS31FL319X_DATA_UPDATE:
  238. case IS31FL319X_TIME_UPDATE:
  239. case IS31FL319X_RESET:
  240. return true; /* always write-through */
  241. default:
  242. return false;
  243. }
  244. }
  245. static const struct reg_default is31fl319x_reg_defaults[] = {
  246. { IS31FL319X_CONFIG1, 0x00},
  247. { IS31FL319X_CONFIG2, 0x00},
  248. { IS31FL319X_PWM(0), 0x00},
  249. { IS31FL319X_PWM(1), 0x00},
  250. { IS31FL319X_PWM(2), 0x00},
  251. { IS31FL319X_PWM(3), 0x00},
  252. { IS31FL319X_PWM(4), 0x00},
  253. { IS31FL319X_PWM(5), 0x00},
  254. { IS31FL319X_PWM(6), 0x00},
  255. { IS31FL319X_PWM(7), 0x00},
  256. { IS31FL319X_PWM(8), 0x00},
  257. };
  258. static struct regmap_config regmap_config = {
  259. .reg_bits = 8,
  260. .val_bits = 8,
  261. .max_register = IS31FL319X_REG_CNT,
  262. .cache_type = REGCACHE_FLAT,
  263. .readable_reg = is31fl319x_readable_reg,
  264. .volatile_reg = is31fl319x_volatile_reg,
  265. .reg_defaults = is31fl319x_reg_defaults,
  266. .num_reg_defaults = ARRAY_SIZE(is31fl319x_reg_defaults),
  267. };
  268. static inline int is31fl319x_microamp_to_cs(struct device *dev, u32 microamp)
  269. { /* round down to nearest supported value (range check done by caller) */
  270. u32 step = microamp / IS31FL319X_CURRENT_STEP;
  271. return ((IS31FL319X_CONFIG2_CS_STEP_REF - step) &
  272. IS31FL319X_CONFIG2_CS_MASK) <<
  273. IS31FL319X_CONFIG2_CS_SHIFT; /* CS encoding */
  274. }
  275. static inline int is31fl319x_db_to_gain(u32 dezibel)
  276. { /* round down to nearest supported value (range check done by caller) */
  277. return dezibel / IS31FL319X_AUDIO_GAIN_DB_STEP;
  278. }
  279. static int is31fl319x_probe(struct i2c_client *client,
  280. const struct i2c_device_id *id)
  281. {
  282. struct is31fl319x_chip *is31;
  283. struct device *dev = &client->dev;
  284. int err;
  285. int i = 0;
  286. u32 aggregated_led_microamp = IS31FL319X_CURRENT_MAX;
  287. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  288. return -EIO;
  289. is31 = devm_kzalloc(&client->dev, sizeof(*is31), GFP_KERNEL);
  290. if (!is31)
  291. return -ENOMEM;
  292. mutex_init(&is31->lock);
  293. err = is31fl319x_parse_dt(&client->dev, is31);
  294. if (err)
  295. goto free_mutex;
  296. if (is31->shutdown_gpio) {
  297. gpiod_direction_output(is31->shutdown_gpio, 0);
  298. mdelay(5);
  299. gpiod_direction_output(is31->shutdown_gpio, 1);
  300. }
  301. is31->client = client;
  302. is31->regmap = devm_regmap_init_i2c(client, &regmap_config);
  303. if (IS_ERR(is31->regmap)) {
  304. dev_err(&client->dev, "failed to allocate register map\n");
  305. err = PTR_ERR(is31->regmap);
  306. goto free_mutex;
  307. }
  308. i2c_set_clientdata(client, is31);
  309. /* check for write-reply from chip (we can't read any registers) */
  310. err = regmap_write(is31->regmap, IS31FL319X_RESET, 0x00);
  311. if (err < 0) {
  312. dev_err(&client->dev, "no response from chip write: err = %d\n",
  313. err);
  314. err = -EIO; /* does not answer */
  315. goto free_mutex;
  316. }
  317. /*
  318. * Kernel conventions require per-LED led-max-microamp property.
  319. * But the chip does not allow to limit individual LEDs.
  320. * So we take minimum from all subnodes for safety of hardware.
  321. */
  322. for (i = 0; i < is31->cdef->num_leds; i++)
  323. if (is31->leds[i].configured &&
  324. is31->leds[i].max_microamp < aggregated_led_microamp)
  325. aggregated_led_microamp = is31->leds[i].max_microamp;
  326. regmap_write(is31->regmap, IS31FL319X_CONFIG2,
  327. is31fl319x_microamp_to_cs(dev, aggregated_led_microamp) |
  328. is31fl319x_db_to_gain(is31->audio_gain_db));
  329. for (i = 0; i < is31->cdef->num_leds; i++) {
  330. struct is31fl319x_led *led = &is31->leds[i];
  331. if (!led->configured)
  332. continue;
  333. led->chip = is31;
  334. led->cdev.brightness_set_blocking = is31fl319x_brightness_set;
  335. err = devm_led_classdev_register(&client->dev, &led->cdev);
  336. if (err < 0)
  337. goto free_mutex;
  338. }
  339. return 0;
  340. free_mutex:
  341. mutex_destroy(&is31->lock);
  342. return err;
  343. }
  344. static int is31fl319x_remove(struct i2c_client *client)
  345. {
  346. struct is31fl319x_chip *is31 = i2c_get_clientdata(client);
  347. mutex_destroy(&is31->lock);
  348. return 0;
  349. }
  350. /*
  351. * i2c-core (and modalias) requires that id_table be properly filled,
  352. * even though it is not used for DeviceTree based instantiation.
  353. */
  354. static const struct i2c_device_id is31fl319x_id[] = {
  355. { "is31fl3190" },
  356. { "is31fl3191" },
  357. { "is31fl3193" },
  358. { "is31fl3196" },
  359. { "is31fl3199" },
  360. { "sn3199" },
  361. {},
  362. };
  363. MODULE_DEVICE_TABLE(i2c, is31fl319x_id);
  364. static struct i2c_driver is31fl319x_driver = {
  365. .driver = {
  366. .name = "leds-is31fl319x",
  367. .of_match_table = of_match_ptr(of_is31fl319x_match),
  368. },
  369. .probe = is31fl319x_probe,
  370. .remove = is31fl319x_remove,
  371. .id_table = is31fl319x_id,
  372. };
  373. module_i2c_driver(is31fl319x_driver);
  374. MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
  375. MODULE_AUTHOR("Andrey Utkin <andrey_utkin@fastmail.com>");
  376. MODULE_DESCRIPTION("IS31FL319X LED driver");
  377. MODULE_LICENSE("GPL v2");