lp855x_bl.c 11 KB

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