lp855x_bl.c 11 KB

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