leds-lm355x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Simple driver for Texas Instruments LM355x LED Flash driver chip
  3. * Copyright (C) 2012 Texas Instruments
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/delay.h>
  11. #include <linux/i2c.h>
  12. #include <linux/gpio.h>
  13. #include <linux/leds.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/fs.h>
  17. #include <linux/regmap.h>
  18. #include <linux/platform_data/leds-lm355x.h>
  19. enum lm355x_type {
  20. CHIP_LM3554 = 0,
  21. CHIP_LM3556,
  22. };
  23. enum lm355x_regs {
  24. REG_FLAG = 0,
  25. REG_TORCH_CFG,
  26. REG_TORCH_CTRL,
  27. REG_STROBE_CFG,
  28. REG_FLASH_CTRL,
  29. REG_INDI_CFG,
  30. REG_INDI_CTRL,
  31. REG_OPMODE,
  32. REG_MAX,
  33. };
  34. /* operation mode */
  35. enum lm355x_mode {
  36. MODE_SHDN = 0,
  37. MODE_INDIC,
  38. MODE_TORCH,
  39. MODE_FLASH
  40. };
  41. /* register map info. */
  42. struct lm355x_reg_data {
  43. u8 regno;
  44. u8 mask;
  45. u8 shift;
  46. };
  47. struct lm355x_chip_data {
  48. struct device *dev;
  49. enum lm355x_type type;
  50. struct led_classdev cdev_flash;
  51. struct led_classdev cdev_torch;
  52. struct led_classdev cdev_indicator;
  53. struct lm355x_platform_data *pdata;
  54. struct regmap *regmap;
  55. struct mutex lock;
  56. unsigned int last_flag;
  57. struct lm355x_reg_data *regs;
  58. };
  59. /* specific indicator function for lm3556 */
  60. enum lm3556_indic_pulse_time {
  61. PULSE_TIME_0_MS = 0,
  62. PULSE_TIME_32_MS,
  63. PULSE_TIME_64_MS,
  64. PULSE_TIME_92_MS,
  65. PULSE_TIME_128_MS,
  66. PULSE_TIME_160_MS,
  67. PULSE_TIME_196_MS,
  68. PULSE_TIME_224_MS,
  69. PULSE_TIME_256_MS,
  70. PULSE_TIME_288_MS,
  71. PULSE_TIME_320_MS,
  72. PULSE_TIME_352_MS,
  73. PULSE_TIME_384_MS,
  74. PULSE_TIME_416_MS,
  75. PULSE_TIME_448_MS,
  76. PULSE_TIME_480_MS,
  77. };
  78. enum lm3556_indic_n_blank {
  79. INDIC_N_BLANK_0 = 0,
  80. INDIC_N_BLANK_1,
  81. INDIC_N_BLANK_2,
  82. INDIC_N_BLANK_3,
  83. INDIC_N_BLANK_4,
  84. INDIC_N_BLANK_5,
  85. INDIC_N_BLANK_6,
  86. INDIC_N_BLANK_7,
  87. INDIC_N_BLANK_8,
  88. INDIC_N_BLANK_9,
  89. INDIC_N_BLANK_10,
  90. INDIC_N_BLANK_11,
  91. INDIC_N_BLANK_12,
  92. INDIC_N_BLANK_13,
  93. INDIC_N_BLANK_14,
  94. INDIC_N_BLANK_15,
  95. };
  96. enum lm3556_indic_period {
  97. INDIC_PERIOD_0 = 0,
  98. INDIC_PERIOD_1,
  99. INDIC_PERIOD_2,
  100. INDIC_PERIOD_3,
  101. INDIC_PERIOD_4,
  102. INDIC_PERIOD_5,
  103. INDIC_PERIOD_6,
  104. INDIC_PERIOD_7,
  105. };
  106. #define INDIC_PATTERN_SIZE 4
  107. struct indicator {
  108. u8 blinking;
  109. u8 period_cnt;
  110. };
  111. /* indicator pattern data only for lm3556 */
  112. static struct indicator indicator_pattern[INDIC_PATTERN_SIZE] = {
  113. [0] = {(INDIC_N_BLANK_1 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_1},
  114. [1] = {(INDIC_N_BLANK_15 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_2},
  115. [2] = {(INDIC_N_BLANK_10 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_4},
  116. [3] = {(INDIC_N_BLANK_5 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_7},
  117. };
  118. static struct lm355x_reg_data lm3554_regs[REG_MAX] = {
  119. [REG_FLAG] = {0xD0, 0xBF, 0},
  120. [REG_TORCH_CFG] = {0xE0, 0x80, 7},
  121. [REG_TORCH_CTRL] = {0xA0, 0x38, 3},
  122. [REG_STROBE_CFG] = {0xE0, 0x04, 2},
  123. [REG_FLASH_CTRL] = {0xB0, 0x78, 3},
  124. [REG_INDI_CFG] = {0xE0, 0x08, 3},
  125. [REG_INDI_CTRL] = {0xA0, 0xC0, 6},
  126. [REG_OPMODE] = {0xA0, 0x03, 0},
  127. };
  128. static struct lm355x_reg_data lm3556_regs[REG_MAX] = {
  129. [REG_FLAG] = {0x0B, 0xFF, 0},
  130. [REG_TORCH_CFG] = {0x0A, 0x10, 4},
  131. [REG_TORCH_CTRL] = {0x09, 0x70, 4},
  132. [REG_STROBE_CFG] = {0x0A, 0x20, 5},
  133. [REG_FLASH_CTRL] = {0x09, 0x0F, 0},
  134. [REG_INDI_CFG] = {0xFF, 0xFF, 0},
  135. [REG_INDI_CTRL] = {0x09, 0x70, 4},
  136. [REG_OPMODE] = {0x0A, 0x03, 0},
  137. };
  138. static char lm355x_name[][I2C_NAME_SIZE] = {
  139. [CHIP_LM3554] = LM3554_NAME,
  140. [CHIP_LM3556] = LM3556_NAME,
  141. };
  142. /* chip initialize */
  143. static int lm355x_chip_init(struct lm355x_chip_data *chip)
  144. {
  145. int ret;
  146. unsigned int reg_val;
  147. struct lm355x_platform_data *pdata = chip->pdata;
  148. /* input and output pins configuration */
  149. switch (chip->type) {
  150. case CHIP_LM3554:
  151. reg_val = pdata->pin_tx2 | pdata->ntc_pin;
  152. ret = regmap_update_bits(chip->regmap, 0xE0, 0x28, reg_val);
  153. if (ret < 0)
  154. goto out;
  155. reg_val = pdata->pass_mode;
  156. ret = regmap_update_bits(chip->regmap, 0xA0, 0x04, reg_val);
  157. if (ret < 0)
  158. goto out;
  159. break;
  160. case CHIP_LM3556:
  161. reg_val = pdata->pin_tx2 | pdata->ntc_pin | pdata->pass_mode;
  162. ret = regmap_update_bits(chip->regmap, 0x0A, 0xC4, reg_val);
  163. if (ret < 0)
  164. goto out;
  165. break;
  166. default:
  167. return -ENODATA;
  168. }
  169. return ret;
  170. out:
  171. dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
  172. return ret;
  173. }
  174. /* chip control */
  175. static int lm355x_control(struct lm355x_chip_data *chip,
  176. u8 brightness, enum lm355x_mode opmode)
  177. {
  178. int ret;
  179. unsigned int reg_val;
  180. struct lm355x_platform_data *pdata = chip->pdata;
  181. struct lm355x_reg_data *preg = chip->regs;
  182. ret = regmap_read(chip->regmap, preg[REG_FLAG].regno, &chip->last_flag);
  183. if (ret < 0)
  184. goto out;
  185. if (chip->last_flag & preg[REG_FLAG].mask)
  186. dev_info(chip->dev, "%s Last FLAG is 0x%x\n",
  187. lm355x_name[chip->type],
  188. chip->last_flag & preg[REG_FLAG].mask);
  189. /* brightness 0 means shutdown */
  190. if (!brightness)
  191. opmode = MODE_SHDN;
  192. switch (opmode) {
  193. case MODE_TORCH:
  194. ret =
  195. regmap_update_bits(chip->regmap, preg[REG_TORCH_CTRL].regno,
  196. preg[REG_TORCH_CTRL].mask,
  197. (brightness - 1)
  198. << preg[REG_TORCH_CTRL].shift);
  199. if (ret < 0)
  200. goto out;
  201. if (pdata->pin_tx1 != LM355x_PIN_TORCH_DISABLE) {
  202. ret =
  203. regmap_update_bits(chip->regmap,
  204. preg[REG_TORCH_CFG].regno,
  205. preg[REG_TORCH_CFG].mask,
  206. 0x01 <<
  207. preg[REG_TORCH_CFG].shift);
  208. if (ret < 0)
  209. goto out;
  210. opmode = MODE_SHDN;
  211. dev_info(chip->dev,
  212. "torch brt is set - ext. torch pin mode\n");
  213. }
  214. break;
  215. case MODE_FLASH:
  216. ret =
  217. regmap_update_bits(chip->regmap, preg[REG_FLASH_CTRL].regno,
  218. preg[REG_FLASH_CTRL].mask,
  219. (brightness - 1)
  220. << preg[REG_FLASH_CTRL].shift);
  221. if (ret < 0)
  222. goto out;
  223. if (pdata->pin_strobe != LM355x_PIN_STROBE_DISABLE) {
  224. if (chip->type == CHIP_LM3554)
  225. reg_val = 0x00;
  226. else
  227. reg_val = 0x01;
  228. ret =
  229. regmap_update_bits(chip->regmap,
  230. preg[REG_STROBE_CFG].regno,
  231. preg[REG_STROBE_CFG].mask,
  232. reg_val <<
  233. preg[REG_STROBE_CFG].shift);
  234. if (ret < 0)
  235. goto out;
  236. opmode = MODE_SHDN;
  237. dev_info(chip->dev,
  238. "flash brt is set - ext. strobe pin mode\n");
  239. }
  240. break;
  241. case MODE_INDIC:
  242. ret =
  243. regmap_update_bits(chip->regmap, preg[REG_INDI_CTRL].regno,
  244. preg[REG_INDI_CTRL].mask,
  245. (brightness - 1)
  246. << preg[REG_INDI_CTRL].shift);
  247. if (ret < 0)
  248. goto out;
  249. if (pdata->pin_tx2 != LM355x_PIN_TX_DISABLE) {
  250. ret =
  251. regmap_update_bits(chip->regmap,
  252. preg[REG_INDI_CFG].regno,
  253. preg[REG_INDI_CFG].mask,
  254. 0x01 <<
  255. preg[REG_INDI_CFG].shift);
  256. if (ret < 0)
  257. goto out;
  258. opmode = MODE_SHDN;
  259. }
  260. break;
  261. case MODE_SHDN:
  262. break;
  263. default:
  264. return -EINVAL;
  265. }
  266. /* operation mode control */
  267. ret = regmap_update_bits(chip->regmap, preg[REG_OPMODE].regno,
  268. preg[REG_OPMODE].mask,
  269. opmode << preg[REG_OPMODE].shift);
  270. if (ret < 0)
  271. goto out;
  272. return ret;
  273. out:
  274. dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
  275. return ret;
  276. }
  277. /* torch */
  278. static int lm355x_torch_brightness_set(struct led_classdev *cdev,
  279. enum led_brightness brightness)
  280. {
  281. struct lm355x_chip_data *chip =
  282. container_of(cdev, struct lm355x_chip_data, cdev_torch);
  283. int ret;
  284. mutex_lock(&chip->lock);
  285. ret = lm355x_control(chip, brightness, MODE_TORCH);
  286. mutex_unlock(&chip->lock);
  287. return ret;
  288. }
  289. /* flash */
  290. static int lm355x_strobe_brightness_set(struct led_classdev *cdev,
  291. enum led_brightness brightness)
  292. {
  293. struct lm355x_chip_data *chip =
  294. container_of(cdev, struct lm355x_chip_data, cdev_flash);
  295. int ret;
  296. mutex_lock(&chip->lock);
  297. ret = lm355x_control(chip, brightness, MODE_FLASH);
  298. mutex_unlock(&chip->lock);
  299. return ret;
  300. }
  301. /* indicator */
  302. static int lm355x_indicator_brightness_set(struct led_classdev *cdev,
  303. enum led_brightness brightness)
  304. {
  305. struct lm355x_chip_data *chip =
  306. container_of(cdev, struct lm355x_chip_data, cdev_indicator);
  307. int ret;
  308. mutex_lock(&chip->lock);
  309. ret = lm355x_control(chip, brightness, MODE_INDIC);
  310. mutex_unlock(&chip->lock);
  311. return ret;
  312. }
  313. /* indicator pattern only for lm3556*/
  314. static ssize_t lm3556_indicator_pattern_store(struct device *dev,
  315. struct device_attribute *attr,
  316. const char *buf, size_t size)
  317. {
  318. ssize_t ret;
  319. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  320. struct lm355x_chip_data *chip =
  321. container_of(led_cdev, struct lm355x_chip_data, cdev_indicator);
  322. unsigned int state;
  323. ret = kstrtouint(buf, 10, &state);
  324. if (ret)
  325. goto out;
  326. if (state > INDIC_PATTERN_SIZE - 1)
  327. state = INDIC_PATTERN_SIZE - 1;
  328. ret = regmap_write(chip->regmap, 0x04,
  329. indicator_pattern[state].blinking);
  330. if (ret < 0)
  331. goto out;
  332. ret = regmap_write(chip->regmap, 0x05,
  333. indicator_pattern[state].period_cnt);
  334. if (ret < 0)
  335. goto out;
  336. return size;
  337. out:
  338. dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
  339. return ret;
  340. }
  341. static DEVICE_ATTR(pattern, S_IWUSR, NULL, lm3556_indicator_pattern_store);
  342. static struct attribute *lm355x_indicator_attrs[] = {
  343. &dev_attr_pattern.attr,
  344. NULL
  345. };
  346. ATTRIBUTE_GROUPS(lm355x_indicator);
  347. static const struct regmap_config lm355x_regmap = {
  348. .reg_bits = 8,
  349. .val_bits = 8,
  350. .max_register = 0xFF,
  351. };
  352. /* module initialize */
  353. static int lm355x_probe(struct i2c_client *client,
  354. const struct i2c_device_id *id)
  355. {
  356. struct lm355x_platform_data *pdata = dev_get_platdata(&client->dev);
  357. struct lm355x_chip_data *chip;
  358. int err;
  359. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  360. dev_err(&client->dev, "i2c functionality check fail.\n");
  361. return -EOPNOTSUPP;
  362. }
  363. if (pdata == NULL) {
  364. dev_err(&client->dev, "needs Platform Data.\n");
  365. return -ENODATA;
  366. }
  367. chip = devm_kzalloc(&client->dev,
  368. sizeof(struct lm355x_chip_data), GFP_KERNEL);
  369. if (!chip)
  370. return -ENOMEM;
  371. chip->dev = &client->dev;
  372. chip->type = id->driver_data;
  373. switch (id->driver_data) {
  374. case CHIP_LM3554:
  375. chip->regs = lm3554_regs;
  376. break;
  377. case CHIP_LM3556:
  378. chip->regs = lm3556_regs;
  379. break;
  380. default:
  381. return -ENOSYS;
  382. }
  383. chip->pdata = pdata;
  384. chip->regmap = devm_regmap_init_i2c(client, &lm355x_regmap);
  385. if (IS_ERR(chip->regmap)) {
  386. err = PTR_ERR(chip->regmap);
  387. dev_err(&client->dev,
  388. "Failed to allocate register map: %d\n", err);
  389. return err;
  390. }
  391. mutex_init(&chip->lock);
  392. i2c_set_clientdata(client, chip);
  393. err = lm355x_chip_init(chip);
  394. if (err < 0)
  395. goto err_out;
  396. /* flash */
  397. chip->cdev_flash.name = "flash";
  398. chip->cdev_flash.max_brightness = 16;
  399. chip->cdev_flash.brightness_set_blocking = lm355x_strobe_brightness_set;
  400. chip->cdev_flash.default_trigger = "flash";
  401. err = led_classdev_register((struct device *)
  402. &client->dev, &chip->cdev_flash);
  403. if (err < 0)
  404. goto err_out;
  405. /* torch */
  406. chip->cdev_torch.name = "torch";
  407. chip->cdev_torch.max_brightness = 8;
  408. chip->cdev_torch.brightness_set_blocking = lm355x_torch_brightness_set;
  409. chip->cdev_torch.default_trigger = "torch";
  410. err = led_classdev_register((struct device *)
  411. &client->dev, &chip->cdev_torch);
  412. if (err < 0)
  413. goto err_create_torch_file;
  414. /* indicator */
  415. chip->cdev_indicator.name = "indicator";
  416. if (id->driver_data == CHIP_LM3554)
  417. chip->cdev_indicator.max_brightness = 4;
  418. else
  419. chip->cdev_indicator.max_brightness = 8;
  420. chip->cdev_indicator.brightness_set_blocking =
  421. lm355x_indicator_brightness_set;
  422. /* indicator pattern control only for LM3556 */
  423. if (id->driver_data == CHIP_LM3556)
  424. chip->cdev_indicator.groups = lm355x_indicator_groups;
  425. err = led_classdev_register((struct device *)
  426. &client->dev, &chip->cdev_indicator);
  427. if (err < 0)
  428. goto err_create_indicator_file;
  429. dev_info(&client->dev, "%s is initialized\n",
  430. lm355x_name[id->driver_data]);
  431. return 0;
  432. err_create_indicator_file:
  433. led_classdev_unregister(&chip->cdev_torch);
  434. err_create_torch_file:
  435. led_classdev_unregister(&chip->cdev_flash);
  436. err_out:
  437. return err;
  438. }
  439. static int lm355x_remove(struct i2c_client *client)
  440. {
  441. struct lm355x_chip_data *chip = i2c_get_clientdata(client);
  442. struct lm355x_reg_data *preg = chip->regs;
  443. regmap_write(chip->regmap, preg[REG_OPMODE].regno, 0);
  444. led_classdev_unregister(&chip->cdev_indicator);
  445. led_classdev_unregister(&chip->cdev_torch);
  446. led_classdev_unregister(&chip->cdev_flash);
  447. dev_info(&client->dev, "%s is removed\n", lm355x_name[chip->type]);
  448. return 0;
  449. }
  450. static const struct i2c_device_id lm355x_id[] = {
  451. {LM3554_NAME, CHIP_LM3554},
  452. {LM3556_NAME, CHIP_LM3556},
  453. {}
  454. };
  455. MODULE_DEVICE_TABLE(i2c, lm355x_id);
  456. static struct i2c_driver lm355x_i2c_driver = {
  457. .driver = {
  458. .name = LM355x_NAME,
  459. .pm = NULL,
  460. },
  461. .probe = lm355x_probe,
  462. .remove = lm355x_remove,
  463. .id_table = lm355x_id,
  464. };
  465. module_i2c_driver(lm355x_i2c_driver);
  466. MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM355x");
  467. MODULE_AUTHOR("Daniel Jeong <[email protected]>");
  468. MODULE_AUTHOR("G.Shark Jeong <[email protected]>");
  469. MODULE_LICENSE("GPL v2");