leds-lm355x.c 14 KB

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