leds-is31fl319x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright 2015-16 Golden Delicious Computers
  3. *
  4. * Author: Nikolaus Schaller <hns@goldelico.com>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * LED driver for the IS31FL319{0,1,3,6,9} to drive 1, 3, 6 or 9 light
  11. * effect LEDs.
  12. *
  13. */
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/leds.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. /* register numbers */
  23. #define IS31FL319X_SHUTDOWN 0x00
  24. #define IS31FL319X_CTRL1 0x01
  25. #define IS31FL319X_CTRL2 0x02
  26. #define IS31FL319X_CONFIG1 0x03
  27. #define IS31FL319X_CONFIG2 0x04
  28. #define IS31FL319X_RAMP_MODE 0x05
  29. #define IS31FL319X_BREATH_MASK 0x06
  30. #define IS31FL319X_PWM(channel) (0x07 + channel)
  31. #define IS31FL319X_DATA_UPDATE 0x10
  32. #define IS31FL319X_T0(channel) (0x11 + channel)
  33. #define IS31FL319X_T123_1 0x1a
  34. #define IS31FL319X_T123_2 0x1b
  35. #define IS31FL319X_T123_3 0x1c
  36. #define IS31FL319X_T4(channel) (0x1d + channel)
  37. #define IS31FL319X_TIME_UPDATE 0x26
  38. #define IS31FL319X_RESET 0xff
  39. #define IS31FL319X_REG_CNT (IS31FL319X_RESET + 1)
  40. #define IS31FL319X_MAX_LEDS 9
  41. /* CS (Current Setting) in CONFIG2 register */
  42. #define IS31FL319X_CONFIG2_CS_SHIFT 4
  43. #define IS31FL319X_CONFIG2_CS_MASK 0x7
  44. #define IS31FL319X_CONFIG2_CS_STEP_REF 12
  45. #define IS31FL319X_CURRENT_MIN ((u32)5000)
  46. #define IS31FL319X_CURRENT_MAX ((u32)40000)
  47. #define IS31FL319X_CURRENT_STEP ((u32)5000)
  48. #define IS31FL319X_CURRENT_DEFAULT ((u32)20000)
  49. /* Audio gain in CONFIG2 register */
  50. #define IS31FL319X_AUDIO_GAIN_DB_MAX ((u32)21)
  51. #define IS31FL319X_AUDIO_GAIN_DB_STEP ((u32)3)
  52. /*
  53. * regmap is used as a cache of chip's register space,
  54. * to avoid reading back brightness values from chip,
  55. * which is known to hang.
  56. */
  57. struct is31fl319x_chip {
  58. const struct is31fl319x_chipdef *cdef;
  59. struct i2c_client *client;
  60. struct regmap *regmap;
  61. struct mutex lock;
  62. u32 audio_gain_db;
  63. struct is31fl319x_led {
  64. struct is31fl319x_chip *chip;
  65. struct led_classdev cdev;
  66. u32 max_microamp;
  67. bool configured;
  68. } leds[IS31FL319X_MAX_LEDS];
  69. };
  70. struct is31fl319x_chipdef {
  71. int num_leds;
  72. };
  73. static const struct is31fl319x_chipdef is31fl3190_cdef = {
  74. .num_leds = 1,
  75. };
  76. static const struct is31fl319x_chipdef is31fl3193_cdef = {
  77. .num_leds = 3,
  78. };
  79. static const struct is31fl319x_chipdef is31fl3196_cdef = {
  80. .num_leds = 6,
  81. };
  82. static const struct is31fl319x_chipdef is31fl3199_cdef = {
  83. .num_leds = 9,
  84. };
  85. static const struct of_device_id of_is31fl319x_match[] = {
  86. { .compatible = "issi,is31fl3190", .data = &is31fl3190_cdef, },
  87. { .compatible = "issi,is31fl3191", .data = &is31fl3190_cdef, },
  88. { .compatible = "issi,is31fl3193", .data = &is31fl3193_cdef, },
  89. { .compatible = "issi,is31fl3196", .data = &is31fl3196_cdef, },
  90. { .compatible = "issi,is31fl3199", .data = &is31fl3199_cdef, },
  91. { .compatible = "si-en,sn3199", .data = &is31fl3199_cdef, },
  92. { }
  93. };
  94. MODULE_DEVICE_TABLE(of, of_is31fl319x_match);
  95. static int is31fl319x_brightness_set(struct led_classdev *cdev,
  96. enum led_brightness brightness)
  97. {
  98. struct is31fl319x_led *led = container_of(cdev, struct is31fl319x_led,
  99. cdev);
  100. struct is31fl319x_chip *is31 = led->chip;
  101. int chan = led - is31->leds;
  102. int ret;
  103. int i;
  104. u8 ctrl1 = 0, ctrl2 = 0;
  105. dev_dbg(&is31->client->dev, "%s %d: %d\n", __func__, chan, brightness);
  106. mutex_lock(&is31->lock);
  107. /* update PWM register */
  108. ret = regmap_write(is31->regmap, IS31FL319X_PWM(chan), brightness);
  109. if (ret < 0)
  110. goto out;
  111. /* read current brightness of all PWM channels */
  112. for (i = 0; i < is31->cdef->num_leds; i++) {
  113. unsigned int pwm_value;
  114. bool on;
  115. /*
  116. * since neither cdev nor the chip can provide
  117. * the current setting, we read from the regmap cache
  118. */
  119. ret = regmap_read(is31->regmap, IS31FL319X_PWM(i), &pwm_value);
  120. dev_dbg(&is31->client->dev, "%s read %d: ret=%d: %d\n",
  121. __func__, i, ret, pwm_value);
  122. on = ret >= 0 && pwm_value > LED_OFF;
  123. if (i < 3)
  124. ctrl1 |= on << i; /* 0..2 => bit 0..2 */
  125. else if (i < 6)
  126. ctrl1 |= on << (i + 1); /* 3..5 => bit 4..6 */
  127. else
  128. ctrl2 |= on << (i - 6); /* 6..8 => bit 0..2 */
  129. }
  130. if (ctrl1 > 0 || ctrl2 > 0) {
  131. dev_dbg(&is31->client->dev, "power up %02x %02x\n",
  132. ctrl1, ctrl2);
  133. regmap_write(is31->regmap, IS31FL319X_CTRL1, ctrl1);
  134. regmap_write(is31->regmap, IS31FL319X_CTRL2, ctrl2);
  135. /* update PWMs */
  136. regmap_write(is31->regmap, IS31FL319X_DATA_UPDATE, 0x00);
  137. /* enable chip from shut down */
  138. ret = regmap_write(is31->regmap, IS31FL319X_SHUTDOWN, 0x01);
  139. } else {
  140. dev_dbg(&is31->client->dev, "power down\n");
  141. /* shut down (no need to clear CTRL1/2) */
  142. ret = regmap_write(is31->regmap, IS31FL319X_SHUTDOWN, 0x00);
  143. }
  144. out:
  145. mutex_unlock(&is31->lock);
  146. return ret;
  147. }
  148. static int is31fl319x_parse_child_dt(const struct device *dev,
  149. const struct device_node *child,
  150. struct is31fl319x_led *led)
  151. {
  152. struct led_classdev *cdev = &led->cdev;
  153. int ret;
  154. if (of_property_read_string(child, "label", &cdev->name))
  155. cdev->name = child->name;
  156. ret = of_property_read_string(child, "linux,default-trigger",
  157. &cdev->default_trigger);
  158. if (ret < 0 && ret != -EINVAL) /* is optional */
  159. return ret;
  160. led->max_microamp = IS31FL319X_CURRENT_DEFAULT;
  161. ret = of_property_read_u32(child, "led-max-microamp",
  162. &led->max_microamp);
  163. if (!ret) {
  164. if (led->max_microamp < IS31FL319X_CURRENT_MIN)
  165. return -EINVAL; /* not supported */
  166. led->max_microamp = min(led->max_microamp,
  167. IS31FL319X_CURRENT_MAX);
  168. }
  169. return 0;
  170. }
  171. static int is31fl319x_parse_dt(struct device *dev,
  172. struct is31fl319x_chip *is31)
  173. {
  174. struct device_node *np = dev->of_node, *child;
  175. const struct of_device_id *of_dev_id;
  176. int count;
  177. int ret;
  178. if (!np)
  179. return -ENODEV;
  180. of_dev_id = of_match_device(of_is31fl319x_match, dev);
  181. if (!of_dev_id) {
  182. dev_err(dev, "Failed to match device with supported chips\n");
  183. return -EINVAL;
  184. }
  185. is31->cdef = of_dev_id->data;
  186. count = of_get_child_count(np);
  187. dev_dbg(dev, "probe %s with %d leds defined in DT\n",
  188. of_dev_id->compatible, 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_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. struct i2c_adapter *adapter = to_i2c_adapter(dev->parent);
  285. int err;
  286. int i = 0;
  287. u32 aggregated_led_microamp = IS31FL319X_CURRENT_MAX;
  288. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  289. return -EIO;
  290. is31 = devm_kzalloc(&client->dev, sizeof(*is31), GFP_KERNEL);
  291. if (!is31)
  292. return -ENOMEM;
  293. mutex_init(&is31->lock);
  294. err = is31fl319x_parse_dt(&client->dev, is31);
  295. if (err)
  296. goto free_mutex;
  297. is31->client = client;
  298. is31->regmap = devm_regmap_init_i2c(client, &regmap_config);
  299. if (IS_ERR(is31->regmap)) {
  300. dev_err(&client->dev, "failed to allocate register map\n");
  301. err = PTR_ERR(is31->regmap);
  302. goto free_mutex;
  303. }
  304. i2c_set_clientdata(client, is31);
  305. /* check for write-reply from chip (we can't read any registers) */
  306. err = regmap_write(is31->regmap, IS31FL319X_RESET, 0x00);
  307. if (err < 0) {
  308. dev_err(&client->dev, "no response from chip write: err = %d\n",
  309. err);
  310. err = -EIO; /* does not answer */
  311. goto free_mutex;
  312. }
  313. /*
  314. * Kernel conventions require per-LED led-max-microamp property.
  315. * But the chip does not allow to limit individual LEDs.
  316. * So we take minimum from all subnodes for safety of hardware.
  317. */
  318. for (i = 0; i < is31->cdef->num_leds; i++)
  319. if (is31->leds[i].configured &&
  320. is31->leds[i].max_microamp < aggregated_led_microamp)
  321. aggregated_led_microamp = is31->leds[i].max_microamp;
  322. regmap_write(is31->regmap, IS31FL319X_CONFIG2,
  323. is31fl319x_microamp_to_cs(dev, aggregated_led_microamp) |
  324. is31fl319x_db_to_gain(is31->audio_gain_db));
  325. for (i = 0; i < is31->cdef->num_leds; i++) {
  326. struct is31fl319x_led *led = &is31->leds[i];
  327. if (!led->configured)
  328. continue;
  329. led->chip = is31;
  330. led->cdev.brightness_set_blocking = is31fl319x_brightness_set;
  331. err = devm_led_classdev_register(&client->dev, &led->cdev);
  332. if (err < 0)
  333. goto free_mutex;
  334. }
  335. return 0;
  336. free_mutex:
  337. mutex_destroy(&is31->lock);
  338. return err;
  339. }
  340. static int is31fl319x_remove(struct i2c_client *client)
  341. {
  342. struct is31fl319x_chip *is31 = i2c_get_clientdata(client);
  343. mutex_destroy(&is31->lock);
  344. return 0;
  345. }
  346. /*
  347. * i2c-core (and modalias) requires that id_table be properly filled,
  348. * even though it is not used for DeviceTree based instantiation.
  349. */
  350. static const struct i2c_device_id is31fl319x_id[] = {
  351. { "is31fl3190" },
  352. { "is31fl3191" },
  353. { "is31fl3193" },
  354. { "is31fl3196" },
  355. { "is31fl3199" },
  356. { "sn3199" },
  357. {},
  358. };
  359. MODULE_DEVICE_TABLE(i2c, is31fl319x_id);
  360. static struct i2c_driver is31fl319x_driver = {
  361. .driver = {
  362. .name = "leds-is31fl319x",
  363. .of_match_table = of_match_ptr(of_is31fl319x_match),
  364. },
  365. .probe = is31fl319x_probe,
  366. .remove = is31fl319x_remove,
  367. .id_table = is31fl319x_id,
  368. };
  369. module_i2c_driver(is31fl319x_driver);
  370. MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
  371. MODULE_AUTHOR("Andrey Utkin <andrey_utkin@fastmail.com>");
  372. MODULE_DESCRIPTION("IS31FL319X LED driver");
  373. MODULE_LICENSE("GPL v2");