lp855x_bl.c 12 KB

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