as3711_bl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * AS3711 PMIC backlight driver, using DCDC Step Up Converters
  3. *
  4. * Copyright (C) 2012 Renesas Electronics Corporation
  5. * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation
  10. */
  11. #include <linux/backlight.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/fb.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/as3711.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. enum as3711_bl_type {
  23. AS3711_BL_SU1,
  24. AS3711_BL_SU2,
  25. };
  26. struct as3711_bl_data {
  27. bool powered;
  28. const char *fb_name;
  29. struct device *fb_dev;
  30. enum as3711_bl_type type;
  31. int brightness;
  32. struct backlight_device *bl;
  33. };
  34. struct as3711_bl_supply {
  35. struct as3711_bl_data su1;
  36. struct as3711_bl_data su2;
  37. const struct as3711_bl_pdata *pdata;
  38. struct as3711 *as3711;
  39. };
  40. static struct as3711_bl_supply *to_supply(struct as3711_bl_data *su)
  41. {
  42. switch (su->type) {
  43. case AS3711_BL_SU1:
  44. return container_of(su, struct as3711_bl_supply, su1);
  45. case AS3711_BL_SU2:
  46. return container_of(su, struct as3711_bl_supply, su2);
  47. }
  48. return NULL;
  49. }
  50. static int as3711_set_brightness_auto_i(struct as3711_bl_data *data,
  51. unsigned int brightness)
  52. {
  53. struct as3711_bl_supply *supply = to_supply(data);
  54. struct as3711 *as3711 = supply->as3711;
  55. const struct as3711_bl_pdata *pdata = supply->pdata;
  56. int ret = 0;
  57. /* Only all equal current values are supported */
  58. if (pdata->su2_auto_curr1)
  59. ret = regmap_write(as3711->regmap, AS3711_CURR1_VALUE,
  60. brightness);
  61. if (!ret && pdata->su2_auto_curr2)
  62. ret = regmap_write(as3711->regmap, AS3711_CURR2_VALUE,
  63. brightness);
  64. if (!ret && pdata->su2_auto_curr3)
  65. ret = regmap_write(as3711->regmap, AS3711_CURR3_VALUE,
  66. brightness);
  67. return ret;
  68. }
  69. static int as3711_set_brightness_v(struct as3711 *as3711,
  70. unsigned int brightness,
  71. unsigned int reg)
  72. {
  73. if (brightness > 31)
  74. return -EINVAL;
  75. return regmap_update_bits(as3711->regmap, reg, 0xf0,
  76. brightness << 4);
  77. }
  78. static int as3711_bl_su2_reset(struct as3711_bl_supply *supply)
  79. {
  80. struct as3711 *as3711 = supply->as3711;
  81. int ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_5,
  82. 3, supply->pdata->su2_fbprot);
  83. if (!ret)
  84. ret = regmap_update_bits(as3711->regmap,
  85. AS3711_STEPUP_CONTROL_2, 1, 0);
  86. if (!ret)
  87. ret = regmap_update_bits(as3711->regmap,
  88. AS3711_STEPUP_CONTROL_2, 1, 1);
  89. return ret;
  90. }
  91. /*
  92. * Someone with less fragile or less expensive hardware could try to simplify
  93. * the brightness adjustment procedure.
  94. */
  95. static int as3711_bl_update_status(struct backlight_device *bl)
  96. {
  97. struct as3711_bl_data *data = bl_get_data(bl);
  98. struct as3711_bl_supply *supply = to_supply(data);
  99. struct as3711 *as3711 = supply->as3711;
  100. int brightness = bl->props.brightness;
  101. int ret = 0;
  102. dev_dbg(&bl->dev, "%s(): brightness %u, pwr %x, blank %x, state %x\n",
  103. __func__, bl->props.brightness, bl->props.power,
  104. bl->props.fb_blank, bl->props.state);
  105. if (bl->props.power != FB_BLANK_UNBLANK ||
  106. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  107. bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  108. brightness = 0;
  109. if (data->type == AS3711_BL_SU1) {
  110. ret = as3711_set_brightness_v(as3711, brightness,
  111. AS3711_STEPUP_CONTROL_1);
  112. } else {
  113. const struct as3711_bl_pdata *pdata = supply->pdata;
  114. switch (pdata->su2_feedback) {
  115. case AS3711_SU2_VOLTAGE:
  116. ret = as3711_set_brightness_v(as3711, brightness,
  117. AS3711_STEPUP_CONTROL_2);
  118. break;
  119. case AS3711_SU2_CURR_AUTO:
  120. ret = as3711_set_brightness_auto_i(data, brightness / 4);
  121. if (ret < 0)
  122. return ret;
  123. if (brightness) {
  124. ret = as3711_bl_su2_reset(supply);
  125. if (ret < 0)
  126. return ret;
  127. udelay(500);
  128. ret = as3711_set_brightness_auto_i(data, brightness);
  129. } else {
  130. ret = regmap_update_bits(as3711->regmap,
  131. AS3711_STEPUP_CONTROL_2, 1, 0);
  132. }
  133. break;
  134. /* Manual one current feedback pin below */
  135. case AS3711_SU2_CURR1:
  136. ret = regmap_write(as3711->regmap, AS3711_CURR1_VALUE,
  137. brightness);
  138. break;
  139. case AS3711_SU2_CURR2:
  140. ret = regmap_write(as3711->regmap, AS3711_CURR2_VALUE,
  141. brightness);
  142. break;
  143. case AS3711_SU2_CURR3:
  144. ret = regmap_write(as3711->regmap, AS3711_CURR3_VALUE,
  145. brightness);
  146. break;
  147. default:
  148. ret = -EINVAL;
  149. }
  150. }
  151. if (!ret)
  152. data->brightness = brightness;
  153. return ret;
  154. }
  155. static int as3711_bl_get_brightness(struct backlight_device *bl)
  156. {
  157. struct as3711_bl_data *data = bl_get_data(bl);
  158. return data->brightness;
  159. }
  160. static const struct backlight_ops as3711_bl_ops = {
  161. .update_status = as3711_bl_update_status,
  162. .get_brightness = as3711_bl_get_brightness,
  163. };
  164. static int as3711_bl_init_su2(struct as3711_bl_supply *supply)
  165. {
  166. struct as3711 *as3711 = supply->as3711;
  167. const struct as3711_bl_pdata *pdata = supply->pdata;
  168. u8 ctl = 0;
  169. int ret;
  170. dev_dbg(as3711->dev, "%s(): use %u\n", __func__, pdata->su2_feedback);
  171. /* Turn SU2 off */
  172. ret = regmap_write(as3711->regmap, AS3711_STEPUP_CONTROL_2, 0);
  173. if (ret < 0)
  174. return ret;
  175. switch (pdata->su2_feedback) {
  176. case AS3711_SU2_VOLTAGE:
  177. ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 0);
  178. break;
  179. case AS3711_SU2_CURR1:
  180. ctl = 1;
  181. ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 1);
  182. break;
  183. case AS3711_SU2_CURR2:
  184. ctl = 4;
  185. ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 2);
  186. break;
  187. case AS3711_SU2_CURR3:
  188. ctl = 0x10;
  189. ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 3);
  190. break;
  191. case AS3711_SU2_CURR_AUTO:
  192. if (pdata->su2_auto_curr1)
  193. ctl = 2;
  194. if (pdata->su2_auto_curr2)
  195. ctl |= 8;
  196. if (pdata->su2_auto_curr3)
  197. ctl |= 0x20;
  198. ret = 0;
  199. break;
  200. default:
  201. return -EINVAL;
  202. }
  203. if (!ret)
  204. ret = regmap_write(as3711->regmap, AS3711_CURR_CONTROL, ctl);
  205. return ret;
  206. }
  207. static int as3711_bl_register(struct platform_device *pdev,
  208. unsigned int max_brightness, struct as3711_bl_data *su)
  209. {
  210. struct backlight_properties props = {.type = BACKLIGHT_RAW,};
  211. struct backlight_device *bl;
  212. /* max tuning I = 31uA for voltage- and 38250uA for current-feedback */
  213. props.max_brightness = max_brightness;
  214. bl = devm_backlight_device_register(&pdev->dev,
  215. su->type == AS3711_BL_SU1 ?
  216. "as3711-su1" : "as3711-su2",
  217. &pdev->dev, su,
  218. &as3711_bl_ops, &props);
  219. if (IS_ERR(bl)) {
  220. dev_err(&pdev->dev, "failed to register backlight\n");
  221. return PTR_ERR(bl);
  222. }
  223. bl->props.brightness = props.max_brightness;
  224. backlight_update_status(bl);
  225. su->bl = bl;
  226. return 0;
  227. }
  228. static int as3711_backlight_parse_dt(struct device *dev)
  229. {
  230. struct as3711_bl_pdata *pdata = dev_get_platdata(dev);
  231. struct device_node *bl =
  232. of_find_node_by_name(dev->parent->of_node, "backlight"), *fb;
  233. int ret;
  234. if (!bl) {
  235. dev_dbg(dev, "backlight node not found\n");
  236. return -ENODEV;
  237. }
  238. fb = of_parse_phandle(bl, "su1-dev", 0);
  239. if (fb) {
  240. pdata->su1_fb = fb->full_name;
  241. ret = of_property_read_u32(bl, "su1-max-uA", &pdata->su1_max_uA);
  242. if (pdata->su1_max_uA <= 0)
  243. ret = -EINVAL;
  244. if (ret < 0)
  245. return ret;
  246. }
  247. fb = of_parse_phandle(bl, "su2-dev", 0);
  248. if (fb) {
  249. int count = 0;
  250. pdata->su2_fb = fb->full_name;
  251. ret = of_property_read_u32(bl, "su2-max-uA", &pdata->su2_max_uA);
  252. if (pdata->su2_max_uA <= 0)
  253. ret = -EINVAL;
  254. if (ret < 0)
  255. return ret;
  256. if (of_find_property(bl, "su2-feedback-voltage", NULL)) {
  257. pdata->su2_feedback = AS3711_SU2_VOLTAGE;
  258. count++;
  259. }
  260. if (of_find_property(bl, "su2-feedback-curr1", NULL)) {
  261. pdata->su2_feedback = AS3711_SU2_CURR1;
  262. count++;
  263. }
  264. if (of_find_property(bl, "su2-feedback-curr2", NULL)) {
  265. pdata->su2_feedback = AS3711_SU2_CURR2;
  266. count++;
  267. }
  268. if (of_find_property(bl, "su2-feedback-curr3", NULL)) {
  269. pdata->su2_feedback = AS3711_SU2_CURR3;
  270. count++;
  271. }
  272. if (of_find_property(bl, "su2-feedback-curr-auto", NULL)) {
  273. pdata->su2_feedback = AS3711_SU2_CURR_AUTO;
  274. count++;
  275. }
  276. if (count != 1)
  277. return -EINVAL;
  278. count = 0;
  279. if (of_find_property(bl, "su2-fbprot-lx-sd4", NULL)) {
  280. pdata->su2_fbprot = AS3711_SU2_LX_SD4;
  281. count++;
  282. }
  283. if (of_find_property(bl, "su2-fbprot-gpio2", NULL)) {
  284. pdata->su2_fbprot = AS3711_SU2_GPIO2;
  285. count++;
  286. }
  287. if (of_find_property(bl, "su2-fbprot-gpio3", NULL)) {
  288. pdata->su2_fbprot = AS3711_SU2_GPIO3;
  289. count++;
  290. }
  291. if (of_find_property(bl, "su2-fbprot-gpio4", NULL)) {
  292. pdata->su2_fbprot = AS3711_SU2_GPIO4;
  293. count++;
  294. }
  295. if (count != 1)
  296. return -EINVAL;
  297. count = 0;
  298. if (of_find_property(bl, "su2-auto-curr1", NULL)) {
  299. pdata->su2_auto_curr1 = true;
  300. count++;
  301. }
  302. if (of_find_property(bl, "su2-auto-curr2", NULL)) {
  303. pdata->su2_auto_curr2 = true;
  304. count++;
  305. }
  306. if (of_find_property(bl, "su2-auto-curr3", NULL)) {
  307. pdata->su2_auto_curr3 = true;
  308. count++;
  309. }
  310. /*
  311. * At least one su2-auto-curr* must be specified iff
  312. * AS3711_SU2_CURR_AUTO is used
  313. */
  314. if (!count ^ (pdata->su2_feedback != AS3711_SU2_CURR_AUTO))
  315. return -EINVAL;
  316. }
  317. return 0;
  318. }
  319. static int as3711_backlight_probe(struct platform_device *pdev)
  320. {
  321. struct as3711_bl_pdata *pdata = dev_get_platdata(&pdev->dev);
  322. struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent);
  323. struct as3711_bl_supply *supply;
  324. struct as3711_bl_data *su;
  325. unsigned int max_brightness;
  326. int ret;
  327. if (!pdata) {
  328. dev_err(&pdev->dev, "No platform data, exiting...\n");
  329. return -ENODEV;
  330. }
  331. if (pdev->dev.parent->of_node) {
  332. ret = as3711_backlight_parse_dt(&pdev->dev);
  333. if (ret < 0) {
  334. dev_err(&pdev->dev, "DT parsing failed: %d\n", ret);
  335. return ret;
  336. }
  337. }
  338. if (!pdata->su1_fb && !pdata->su2_fb) {
  339. dev_err(&pdev->dev, "No framebuffer specified\n");
  340. return -EINVAL;
  341. }
  342. /*
  343. * Due to possible hardware damage I chose to block all modes,
  344. * unsupported on my hardware. Anyone, wishing to use any of those modes
  345. * will have to first review the code, then activate and test it.
  346. */
  347. if (pdata->su1_fb ||
  348. pdata->su2_fbprot != AS3711_SU2_GPIO4 ||
  349. pdata->su2_feedback != AS3711_SU2_CURR_AUTO) {
  350. dev_warn(&pdev->dev,
  351. "Attention! An untested mode has been chosen!\n"
  352. "Please, review the code, enable, test, and report success:-)\n");
  353. return -EINVAL;
  354. }
  355. supply = devm_kzalloc(&pdev->dev, sizeof(*supply), GFP_KERNEL);
  356. if (!supply)
  357. return -ENOMEM;
  358. supply->as3711 = as3711;
  359. supply->pdata = pdata;
  360. if (pdata->su1_fb) {
  361. su = &supply->su1;
  362. su->fb_name = pdata->su1_fb;
  363. su->type = AS3711_BL_SU1;
  364. max_brightness = min(pdata->su1_max_uA, 31);
  365. ret = as3711_bl_register(pdev, max_brightness, su);
  366. if (ret < 0)
  367. return ret;
  368. }
  369. if (pdata->su2_fb) {
  370. su = &supply->su2;
  371. su->fb_name = pdata->su2_fb;
  372. su->type = AS3711_BL_SU2;
  373. switch (pdata->su2_fbprot) {
  374. case AS3711_SU2_GPIO2:
  375. case AS3711_SU2_GPIO3:
  376. case AS3711_SU2_GPIO4:
  377. case AS3711_SU2_LX_SD4:
  378. break;
  379. default:
  380. return -EINVAL;
  381. }
  382. switch (pdata->su2_feedback) {
  383. case AS3711_SU2_VOLTAGE:
  384. max_brightness = min(pdata->su2_max_uA, 31);
  385. break;
  386. case AS3711_SU2_CURR1:
  387. case AS3711_SU2_CURR2:
  388. case AS3711_SU2_CURR3:
  389. case AS3711_SU2_CURR_AUTO:
  390. max_brightness = min(pdata->su2_max_uA / 150, 255);
  391. break;
  392. default:
  393. return -EINVAL;
  394. }
  395. ret = as3711_bl_init_su2(supply);
  396. if (ret < 0)
  397. return ret;
  398. ret = as3711_bl_register(pdev, max_brightness, su);
  399. if (ret < 0)
  400. return ret;
  401. }
  402. platform_set_drvdata(pdev, supply);
  403. return 0;
  404. }
  405. static struct platform_driver as3711_backlight_driver = {
  406. .driver = {
  407. .name = "as3711-backlight",
  408. },
  409. .probe = as3711_backlight_probe,
  410. };
  411. module_platform_driver(as3711_backlight_driver);
  412. MODULE_DESCRIPTION("Backlight Driver for AS3711 PMICs");
  413. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de");
  414. MODULE_LICENSE("GPL");
  415. MODULE_ALIAS("platform:as3711-backlight");