lp855x_bl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * TI LP855x Backlight Driver
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <linux/backlight.h>
  15. #include <linux/err.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_data/lp855x.h>
  18. #include <linux/pwm.h>
  19. #include <linux/regulator/consumer.h>
  20. /* LP8550/1/2/3/6 Registers */
  21. #define LP855X_BRIGHTNESS_CTRL 0x00
  22. #define LP855X_DEVICE_CTRL 0x01
  23. #define LP855X_EEPROM_START 0xA0
  24. #define LP855X_EEPROM_END 0xA7
  25. #define LP8556_EPROM_START 0xA0
  26. #define LP8556_EPROM_END 0xAF
  27. /* LP8555/7 Registers */
  28. #define LP8557_BL_CMD 0x00
  29. #define LP8557_BL_MASK 0x01
  30. #define LP8557_BL_ON 0x01
  31. #define LP8557_BL_OFF 0x00
  32. #define LP8557_BRIGHTNESS_CTRL 0x04
  33. #define LP8557_CONFIG 0x10
  34. #define LP8555_EPROM_START 0x10
  35. #define LP8555_EPROM_END 0x7A
  36. #define LP8557_EPROM_START 0x10
  37. #define LP8557_EPROM_END 0x1E
  38. #define DEFAULT_BL_NAME "lcd-backlight"
  39. #define MAX_BRIGHTNESS 255
  40. enum lp855x_brightness_ctrl_mode {
  41. PWM_BASED = 1,
  42. REGISTER_BASED,
  43. };
  44. struct lp855x;
  45. /*
  46. * struct lp855x_device_config
  47. * @pre_init_device: init device function call before updating the brightness
  48. * @reg_brightness: register address for brigthenss control
  49. * @reg_devicectrl: register address for device control
  50. * @post_init_device: late init device function call
  51. */
  52. struct lp855x_device_config {
  53. int (*pre_init_device)(struct lp855x *);
  54. u8 reg_brightness;
  55. u8 reg_devicectrl;
  56. int (*post_init_device)(struct lp855x *);
  57. };
  58. struct lp855x {
  59. const char *chipname;
  60. enum lp855x_chip_id chip_id;
  61. enum lp855x_brightness_ctrl_mode mode;
  62. struct lp855x_device_config *cfg;
  63. struct i2c_client *client;
  64. struct backlight_device *bl;
  65. struct device *dev;
  66. struct lp855x_platform_data *pdata;
  67. struct pwm_device *pwm;
  68. };
  69. static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
  70. {
  71. return i2c_smbus_write_byte_data(lp->client, reg, data);
  72. }
  73. static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data)
  74. {
  75. int ret;
  76. u8 tmp;
  77. ret = i2c_smbus_read_byte_data(lp->client, reg);
  78. if (ret < 0) {
  79. dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
  80. return ret;
  81. }
  82. tmp = (u8)ret;
  83. tmp &= ~mask;
  84. tmp |= data & mask;
  85. return lp855x_write_byte(lp, reg, tmp);
  86. }
  87. static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
  88. {
  89. u8 start, end;
  90. switch (lp->chip_id) {
  91. case LP8550:
  92. case LP8551:
  93. case LP8552:
  94. case LP8553:
  95. start = LP855X_EEPROM_START;
  96. end = LP855X_EEPROM_END;
  97. break;
  98. case LP8556:
  99. start = LP8556_EPROM_START;
  100. end = LP8556_EPROM_END;
  101. break;
  102. case LP8555:
  103. start = LP8555_EPROM_START;
  104. end = LP8555_EPROM_END;
  105. break;
  106. case LP8557:
  107. start = LP8557_EPROM_START;
  108. end = LP8557_EPROM_END;
  109. break;
  110. default:
  111. return false;
  112. }
  113. return addr >= start && addr <= end;
  114. }
  115. static int lp8557_bl_off(struct lp855x *lp)
  116. {
  117. /* BL_ON = 0 before updating EPROM settings */
  118. return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
  119. LP8557_BL_OFF);
  120. }
  121. static int lp8557_bl_on(struct lp855x *lp)
  122. {
  123. /* BL_ON = 1 after updating EPROM settings */
  124. return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
  125. LP8557_BL_ON);
  126. }
  127. static struct lp855x_device_config lp855x_dev_cfg = {
  128. .reg_brightness = LP855X_BRIGHTNESS_CTRL,
  129. .reg_devicectrl = LP855X_DEVICE_CTRL,
  130. };
  131. static struct lp855x_device_config lp8557_dev_cfg = {
  132. .reg_brightness = LP8557_BRIGHTNESS_CTRL,
  133. .reg_devicectrl = LP8557_CONFIG,
  134. .pre_init_device = lp8557_bl_off,
  135. .post_init_device = lp8557_bl_on,
  136. };
  137. /*
  138. * Device specific configuration flow
  139. *
  140. * a) pre_init_device(optional)
  141. * b) update the brightness register
  142. * c) update device control register
  143. * d) update ROM area(optional)
  144. * e) post_init_device(optional)
  145. *
  146. */
  147. static int lp855x_configure(struct lp855x *lp)
  148. {
  149. u8 val, addr;
  150. int i, ret;
  151. struct lp855x_platform_data *pd = lp->pdata;
  152. switch (lp->chip_id) {
  153. case LP8550:
  154. case LP8551:
  155. case LP8552:
  156. case LP8553:
  157. case LP8556:
  158. lp->cfg = &lp855x_dev_cfg;
  159. break;
  160. case LP8555:
  161. case LP8557:
  162. lp->cfg = &lp8557_dev_cfg;
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. if (lp->cfg->pre_init_device) {
  168. ret = lp->cfg->pre_init_device(lp);
  169. if (ret) {
  170. dev_err(lp->dev, "pre init device err: %d\n", ret);
  171. goto err;
  172. }
  173. }
  174. val = pd->initial_brightness;
  175. ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
  176. if (ret)
  177. goto err;
  178. val = pd->device_control;
  179. ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
  180. if (ret)
  181. goto err;
  182. if (pd->size_program > 0) {
  183. for (i = 0; i < pd->size_program; i++) {
  184. addr = pd->rom_data[i].addr;
  185. val = pd->rom_data[i].val;
  186. if (!lp855x_is_valid_rom_area(lp, addr))
  187. continue;
  188. ret = lp855x_write_byte(lp, addr, val);
  189. if (ret)
  190. goto err;
  191. }
  192. }
  193. if (lp->cfg->post_init_device) {
  194. ret = lp->cfg->post_init_device(lp);
  195. if (ret) {
  196. dev_err(lp->dev, "post init device err: %d\n", ret);
  197. goto err;
  198. }
  199. }
  200. return 0;
  201. err:
  202. return ret;
  203. }
  204. static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
  205. {
  206. unsigned int period = lp->pdata->period_ns;
  207. unsigned int duty = br * period / max_br;
  208. struct pwm_device *pwm;
  209. /* request pwm device with the consumer name */
  210. if (!lp->pwm) {
  211. pwm = devm_pwm_get(lp->dev, lp->chipname);
  212. if (IS_ERR(pwm))
  213. return;
  214. lp->pwm = pwm;
  215. }
  216. pwm_config(lp->pwm, duty, period);
  217. if (duty)
  218. pwm_enable(lp->pwm);
  219. else
  220. pwm_disable(lp->pwm);
  221. }
  222. static int lp855x_bl_update_status(struct backlight_device *bl)
  223. {
  224. struct lp855x *lp = bl_get_data(bl);
  225. int brightness = bl->props.brightness;
  226. if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  227. brightness = 0;
  228. if (lp->mode == PWM_BASED)
  229. lp855x_pwm_ctrl(lp, brightness, bl->props.max_brightness);
  230. else if (lp->mode == REGISTER_BASED)
  231. lp855x_write_byte(lp, lp->cfg->reg_brightness, (u8)brightness);
  232. return 0;
  233. }
  234. static const struct backlight_ops lp855x_bl_ops = {
  235. .options = BL_CORE_SUSPENDRESUME,
  236. .update_status = lp855x_bl_update_status,
  237. };
  238. static int lp855x_backlight_register(struct lp855x *lp)
  239. {
  240. struct backlight_device *bl;
  241. struct backlight_properties props;
  242. struct lp855x_platform_data *pdata = lp->pdata;
  243. const char *name = pdata->name ? : DEFAULT_BL_NAME;
  244. props.type = BACKLIGHT_PLATFORM;
  245. props.max_brightness = MAX_BRIGHTNESS;
  246. if (pdata->initial_brightness > props.max_brightness)
  247. pdata->initial_brightness = props.max_brightness;
  248. props.brightness = pdata->initial_brightness;
  249. bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp,
  250. &lp855x_bl_ops, &props);
  251. if (IS_ERR(bl))
  252. return PTR_ERR(bl);
  253. lp->bl = bl;
  254. return 0;
  255. }
  256. static ssize_t lp855x_get_chip_id(struct device *dev,
  257. struct device_attribute *attr, char *buf)
  258. {
  259. struct lp855x *lp = dev_get_drvdata(dev);
  260. return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
  261. }
  262. static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
  263. struct device_attribute *attr, char *buf)
  264. {
  265. struct lp855x *lp = dev_get_drvdata(dev);
  266. char *strmode = NULL;
  267. if (lp->mode == PWM_BASED)
  268. strmode = "pwm based";
  269. else if (lp->mode == REGISTER_BASED)
  270. strmode = "register based";
  271. return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
  272. }
  273. static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
  274. static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
  275. static struct attribute *lp855x_attributes[] = {
  276. &dev_attr_chip_id.attr,
  277. &dev_attr_bl_ctl_mode.attr,
  278. NULL,
  279. };
  280. static const struct attribute_group lp855x_attr_group = {
  281. .attrs = lp855x_attributes,
  282. };
  283. #ifdef CONFIG_OF
  284. static int lp855x_parse_dt(struct lp855x *lp)
  285. {
  286. struct device *dev = lp->dev;
  287. struct device_node *node = dev->of_node;
  288. struct lp855x_platform_data *pdata;
  289. int rom_length;
  290. if (!node) {
  291. dev_err(dev, "no platform data\n");
  292. return -EINVAL;
  293. }
  294. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  295. if (!pdata)
  296. return -ENOMEM;
  297. of_property_read_string(node, "bl-name", &pdata->name);
  298. of_property_read_u8(node, "dev-ctrl", &pdata->device_control);
  299. of_property_read_u8(node, "init-brt", &pdata->initial_brightness);
  300. of_property_read_u32(node, "pwm-period", &pdata->period_ns);
  301. /* Fill ROM platform data if defined */
  302. rom_length = of_get_child_count(node);
  303. if (rom_length > 0) {
  304. struct lp855x_rom_data *rom;
  305. struct device_node *child;
  306. int i = 0;
  307. rom = devm_kzalloc(dev, sizeof(*rom) * rom_length, GFP_KERNEL);
  308. if (!rom)
  309. return -ENOMEM;
  310. for_each_child_of_node(node, child) {
  311. of_property_read_u8(child, "rom-addr", &rom[i].addr);
  312. of_property_read_u8(child, "rom-val", &rom[i].val);
  313. i++;
  314. }
  315. pdata->size_program = rom_length;
  316. pdata->rom_data = &rom[0];
  317. }
  318. pdata->supply = devm_regulator_get(dev, "power");
  319. if (IS_ERR(pdata->supply)) {
  320. if (PTR_ERR(pdata->supply) == -EPROBE_DEFER)
  321. return -EPROBE_DEFER;
  322. pdata->supply = NULL;
  323. }
  324. lp->pdata = pdata;
  325. return 0;
  326. }
  327. #else
  328. static int lp855x_parse_dt(struct lp855x *lp)
  329. {
  330. return -EINVAL;
  331. }
  332. #endif
  333. static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
  334. {
  335. struct lp855x *lp;
  336. int ret;
  337. if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
  338. return -EIO;
  339. lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
  340. if (!lp)
  341. return -ENOMEM;
  342. lp->client = cl;
  343. lp->dev = &cl->dev;
  344. lp->chipname = id->name;
  345. lp->chip_id = id->driver_data;
  346. lp->pdata = dev_get_platdata(&cl->dev);
  347. if (!lp->pdata) {
  348. ret = lp855x_parse_dt(lp);
  349. if (ret < 0)
  350. return ret;
  351. }
  352. if (lp->pdata->period_ns > 0)
  353. lp->mode = PWM_BASED;
  354. else
  355. lp->mode = REGISTER_BASED;
  356. if (lp->pdata->supply) {
  357. ret = regulator_enable(lp->pdata->supply);
  358. if (ret < 0) {
  359. dev_err(&cl->dev, "failed to enable supply: %d\n", ret);
  360. return ret;
  361. }
  362. }
  363. i2c_set_clientdata(cl, lp);
  364. ret = lp855x_configure(lp);
  365. if (ret) {
  366. dev_err(lp->dev, "device config err: %d", ret);
  367. return ret;
  368. }
  369. ret = lp855x_backlight_register(lp);
  370. if (ret) {
  371. dev_err(lp->dev,
  372. "failed to register backlight. err: %d\n", ret);
  373. return ret;
  374. }
  375. ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
  376. if (ret) {
  377. dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
  378. return ret;
  379. }
  380. backlight_update_status(lp->bl);
  381. return 0;
  382. }
  383. static int lp855x_remove(struct i2c_client *cl)
  384. {
  385. struct lp855x *lp = i2c_get_clientdata(cl);
  386. lp->bl->props.brightness = 0;
  387. backlight_update_status(lp->bl);
  388. if (lp->pdata->supply)
  389. regulator_disable(lp->pdata->supply);
  390. sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
  391. return 0;
  392. }
  393. static const struct of_device_id lp855x_dt_ids[] = {
  394. { .compatible = "ti,lp8550", },
  395. { .compatible = "ti,lp8551", },
  396. { .compatible = "ti,lp8552", },
  397. { .compatible = "ti,lp8553", },
  398. { .compatible = "ti,lp8555", },
  399. { .compatible = "ti,lp8556", },
  400. { .compatible = "ti,lp8557", },
  401. { }
  402. };
  403. MODULE_DEVICE_TABLE(of, lp855x_dt_ids);
  404. static const struct i2c_device_id lp855x_ids[] = {
  405. {"lp8550", LP8550},
  406. {"lp8551", LP8551},
  407. {"lp8552", LP8552},
  408. {"lp8553", LP8553},
  409. {"lp8555", LP8555},
  410. {"lp8556", LP8556},
  411. {"lp8557", LP8557},
  412. { }
  413. };
  414. MODULE_DEVICE_TABLE(i2c, lp855x_ids);
  415. static struct i2c_driver lp855x_driver = {
  416. .driver = {
  417. .name = "lp855x",
  418. .of_match_table = of_match_ptr(lp855x_dt_ids),
  419. },
  420. .probe = lp855x_probe,
  421. .remove = lp855x_remove,
  422. .id_table = lp855x_ids,
  423. };
  424. module_i2c_driver(lp855x_driver);
  425. MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
  426. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  427. MODULE_LICENSE("GPL");