lp855x_bl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/platform_data/lp855x.h>
  17. #include <linux/pwm.h>
  18. /* Registers */
  19. #define BRIGHTNESS_CTRL 0x00
  20. #define DEVICE_CTRL 0x01
  21. #define EEPROM_START 0xA0
  22. #define EEPROM_END 0xA7
  23. #define EPROM_START 0xA0
  24. #define EPROM_END 0xAF
  25. #define BUF_SIZE 20
  26. #define DEFAULT_BL_NAME "lcd-backlight"
  27. #define MAX_BRIGHTNESS 255
  28. struct lp855x;
  29. /*
  30. * struct lp855x_device_config
  31. * @pre_init_device: init device function call before updating the brightness
  32. * @reg_brightness: register address for brigthenss control
  33. * @reg_devicectrl: register address for device control
  34. * @post_init_device: late init device function call
  35. */
  36. struct lp855x_device_config {
  37. int (*pre_init_device)(struct lp855x *);
  38. u8 reg_brightness;
  39. u8 reg_devicectrl;
  40. int (*post_init_device)(struct lp855x *);
  41. };
  42. struct lp855x {
  43. const char *chipname;
  44. enum lp855x_chip_id chip_id;
  45. struct lp855x_device_config *cfg;
  46. struct i2c_client *client;
  47. struct backlight_device *bl;
  48. struct device *dev;
  49. struct lp855x_platform_data *pdata;
  50. struct pwm_device *pwm;
  51. };
  52. static int lp855x_read_byte(struct lp855x *lp, u8 reg, u8 *data)
  53. {
  54. int ret;
  55. ret = i2c_smbus_read_byte_data(lp->client, reg);
  56. if (ret < 0) {
  57. dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
  58. return ret;
  59. }
  60. *data = (u8)ret;
  61. return 0;
  62. }
  63. static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
  64. {
  65. return i2c_smbus_write_byte_data(lp->client, reg, data);
  66. }
  67. static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
  68. {
  69. u8 start, end;
  70. switch (lp->chip_id) {
  71. case LP8550:
  72. case LP8551:
  73. case LP8552:
  74. case LP8553:
  75. start = EEPROM_START;
  76. end = EEPROM_END;
  77. break;
  78. case LP8556:
  79. start = EPROM_START;
  80. end = EPROM_END;
  81. break;
  82. default:
  83. return false;
  84. }
  85. return (addr >= start && addr <= end);
  86. }
  87. static struct lp855x_device_config lp855x_dev_cfg = {
  88. .reg_brightness = BRIGHTNESS_CTRL,
  89. .reg_devicectrl = DEVICE_CTRL,
  90. };
  91. /*
  92. * Device specific configuration flow
  93. *
  94. * a) pre_init_device(optional)
  95. * b) update the brightness register
  96. * c) update device control register
  97. * d) update ROM area(optional)
  98. * e) post_init_device(optional)
  99. *
  100. */
  101. static int lp855x_configure(struct lp855x *lp)
  102. {
  103. u8 val, addr;
  104. int i, ret;
  105. struct lp855x_platform_data *pd = lp->pdata;
  106. switch (lp->chip_id) {
  107. case LP8550 ... LP8556:
  108. lp->cfg = &lp855x_dev_cfg;
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. if (lp->cfg->pre_init_device) {
  114. ret = lp->cfg->pre_init_device(lp);
  115. if (ret) {
  116. dev_err(lp->dev, "pre init device err: %d\n", ret);
  117. goto err;
  118. }
  119. }
  120. val = pd->initial_brightness;
  121. ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
  122. if (ret)
  123. goto err;
  124. val = pd->device_control;
  125. ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
  126. if (ret)
  127. goto err;
  128. if (pd->load_new_rom_data && pd->size_program) {
  129. for (i = 0; i < pd->size_program; i++) {
  130. addr = pd->rom_data[i].addr;
  131. val = pd->rom_data[i].val;
  132. if (!lp855x_is_valid_rom_area(lp, addr))
  133. continue;
  134. ret = lp855x_write_byte(lp, addr, val);
  135. if (ret)
  136. goto err;
  137. }
  138. }
  139. if (lp->cfg->post_init_device) {
  140. ret = lp->cfg->post_init_device(lp);
  141. if (ret) {
  142. dev_err(lp->dev, "post init device err: %d\n", ret);
  143. goto err;
  144. }
  145. }
  146. return 0;
  147. err:
  148. return ret;
  149. }
  150. static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
  151. {
  152. unsigned int period = lp->pdata->period_ns;
  153. unsigned int duty = br * period / max_br;
  154. struct pwm_device *pwm;
  155. /* request pwm device with the consumer name */
  156. if (!lp->pwm) {
  157. pwm = devm_pwm_get(lp->dev, lp->chipname);
  158. if (IS_ERR(pwm))
  159. return;
  160. lp->pwm = pwm;
  161. }
  162. pwm_config(lp->pwm, duty, period);
  163. if (duty)
  164. pwm_enable(lp->pwm);
  165. else
  166. pwm_disable(lp->pwm);
  167. }
  168. static int lp855x_bl_update_status(struct backlight_device *bl)
  169. {
  170. struct lp855x *lp = bl_get_data(bl);
  171. enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
  172. if (bl->props.state & BL_CORE_SUSPENDED)
  173. bl->props.brightness = 0;
  174. if (mode == PWM_BASED) {
  175. int br = bl->props.brightness;
  176. int max_br = bl->props.max_brightness;
  177. lp855x_pwm_ctrl(lp, br, max_br);
  178. } else if (mode == REGISTER_BASED) {
  179. u8 val = bl->props.brightness;
  180. lp855x_write_byte(lp, BRIGHTNESS_CTRL, val);
  181. }
  182. return 0;
  183. }
  184. static int lp855x_bl_get_brightness(struct backlight_device *bl)
  185. {
  186. struct lp855x *lp = bl_get_data(bl);
  187. enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
  188. if (mode == REGISTER_BASED) {
  189. u8 val = 0;
  190. lp855x_read_byte(lp, BRIGHTNESS_CTRL, &val);
  191. bl->props.brightness = val;
  192. }
  193. return bl->props.brightness;
  194. }
  195. static const struct backlight_ops lp855x_bl_ops = {
  196. .options = BL_CORE_SUSPENDRESUME,
  197. .update_status = lp855x_bl_update_status,
  198. .get_brightness = lp855x_bl_get_brightness,
  199. };
  200. static int lp855x_backlight_register(struct lp855x *lp)
  201. {
  202. struct backlight_device *bl;
  203. struct backlight_properties props;
  204. struct lp855x_platform_data *pdata = lp->pdata;
  205. char *name = pdata->name ? : DEFAULT_BL_NAME;
  206. props.type = BACKLIGHT_PLATFORM;
  207. props.max_brightness = MAX_BRIGHTNESS;
  208. if (pdata->initial_brightness > props.max_brightness)
  209. pdata->initial_brightness = props.max_brightness;
  210. props.brightness = pdata->initial_brightness;
  211. bl = backlight_device_register(name, lp->dev, lp,
  212. &lp855x_bl_ops, &props);
  213. if (IS_ERR(bl))
  214. return PTR_ERR(bl);
  215. lp->bl = bl;
  216. return 0;
  217. }
  218. static void lp855x_backlight_unregister(struct lp855x *lp)
  219. {
  220. if (lp->bl)
  221. backlight_device_unregister(lp->bl);
  222. }
  223. static ssize_t lp855x_get_chip_id(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. struct lp855x *lp = dev_get_drvdata(dev);
  227. return scnprintf(buf, BUF_SIZE, "%s\n", lp->chipname);
  228. }
  229. static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
  230. struct device_attribute *attr, char *buf)
  231. {
  232. struct lp855x *lp = dev_get_drvdata(dev);
  233. enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
  234. char *strmode = NULL;
  235. if (mode == PWM_BASED)
  236. strmode = "pwm based";
  237. else if (mode == REGISTER_BASED)
  238. strmode = "register based";
  239. return scnprintf(buf, BUF_SIZE, "%s\n", strmode);
  240. }
  241. static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
  242. static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
  243. static struct attribute *lp855x_attributes[] = {
  244. &dev_attr_chip_id.attr,
  245. &dev_attr_bl_ctl_mode.attr,
  246. NULL,
  247. };
  248. static const struct attribute_group lp855x_attr_group = {
  249. .attrs = lp855x_attributes,
  250. };
  251. static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
  252. {
  253. struct lp855x *lp;
  254. struct lp855x_platform_data *pdata = cl->dev.platform_data;
  255. enum lp855x_brightness_ctrl_mode mode;
  256. int ret;
  257. if (!pdata) {
  258. dev_err(&cl->dev, "no platform data supplied\n");
  259. return -EINVAL;
  260. }
  261. if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
  262. return -EIO;
  263. lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
  264. if (!lp)
  265. return -ENOMEM;
  266. mode = pdata->mode;
  267. lp->client = cl;
  268. lp->dev = &cl->dev;
  269. lp->pdata = pdata;
  270. lp->chipname = id->name;
  271. lp->chip_id = id->driver_data;
  272. i2c_set_clientdata(cl, lp);
  273. ret = lp855x_configure(lp);
  274. if (ret) {
  275. dev_err(lp->dev, "device config err: %d", ret);
  276. goto err_dev;
  277. }
  278. ret = lp855x_backlight_register(lp);
  279. if (ret) {
  280. dev_err(lp->dev,
  281. "failed to register backlight. err: %d\n", ret);
  282. goto err_dev;
  283. }
  284. ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
  285. if (ret) {
  286. dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
  287. goto err_sysfs;
  288. }
  289. backlight_update_status(lp->bl);
  290. return 0;
  291. err_sysfs:
  292. lp855x_backlight_unregister(lp);
  293. err_dev:
  294. return ret;
  295. }
  296. static int lp855x_remove(struct i2c_client *cl)
  297. {
  298. struct lp855x *lp = i2c_get_clientdata(cl);
  299. lp->bl->props.brightness = 0;
  300. backlight_update_status(lp->bl);
  301. sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
  302. lp855x_backlight_unregister(lp);
  303. return 0;
  304. }
  305. static const struct i2c_device_id lp855x_ids[] = {
  306. {"lp8550", LP8550},
  307. {"lp8551", LP8551},
  308. {"lp8552", LP8552},
  309. {"lp8553", LP8553},
  310. {"lp8556", LP8556},
  311. { }
  312. };
  313. MODULE_DEVICE_TABLE(i2c, lp855x_ids);
  314. static struct i2c_driver lp855x_driver = {
  315. .driver = {
  316. .name = "lp855x",
  317. },
  318. .probe = lp855x_probe,
  319. .remove = lp855x_remove,
  320. .id_table = lp855x_ids,
  321. };
  322. module_i2c_driver(lp855x_driver);
  323. MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
  324. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  325. MODULE_LICENSE("GPL");