lm3533_bl.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * lm3533-bl.c -- LM3533 Backlight driver
  3. *
  4. * Copyright (C) 2011-2012 Texas Instruments
  5. *
  6. * Author: Johan Hovold <jhovold@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/backlight.h>
  17. #include <linux/fb.h>
  18. #include <linux/slab.h>
  19. #include <linux/mfd/lm3533.h>
  20. #define LM3533_HVCTRLBANK_COUNT 2
  21. #define LM3533_BL_MAX_BRIGHTNESS 255
  22. #define LM3533_REG_CTRLBANK_AB_BCONF 0x1a
  23. struct lm3533_bl {
  24. struct lm3533 *lm3533;
  25. struct lm3533_ctrlbank cb;
  26. struct backlight_device *bd;
  27. int id;
  28. };
  29. static inline int lm3533_bl_get_ctrlbank_id(struct lm3533_bl *bl)
  30. {
  31. return bl->id;
  32. }
  33. static int lm3533_bl_update_status(struct backlight_device *bd)
  34. {
  35. struct lm3533_bl *bl = bl_get_data(bd);
  36. int brightness = bd->props.brightness;
  37. if (bd->props.power != FB_BLANK_UNBLANK)
  38. brightness = 0;
  39. if (bd->props.fb_blank != FB_BLANK_UNBLANK)
  40. brightness = 0;
  41. return lm3533_ctrlbank_set_brightness(&bl->cb, (u8)brightness);
  42. }
  43. static int lm3533_bl_get_brightness(struct backlight_device *bd)
  44. {
  45. struct lm3533_bl *bl = bl_get_data(bd);
  46. u8 val;
  47. int ret;
  48. ret = lm3533_ctrlbank_get_brightness(&bl->cb, &val);
  49. if (ret)
  50. return ret;
  51. return val;
  52. }
  53. static const struct backlight_ops lm3533_bl_ops = {
  54. .get_brightness = lm3533_bl_get_brightness,
  55. .update_status = lm3533_bl_update_status,
  56. };
  57. static ssize_t show_id(struct device *dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. struct lm3533_bl *bl = dev_get_drvdata(dev);
  61. return scnprintf(buf, PAGE_SIZE, "%d\n", bl->id);
  62. }
  63. static ssize_t show_als_channel(struct device *dev,
  64. struct device_attribute *attr, char *buf)
  65. {
  66. struct lm3533_bl *bl = dev_get_drvdata(dev);
  67. unsigned channel = lm3533_bl_get_ctrlbank_id(bl);
  68. return scnprintf(buf, PAGE_SIZE, "%u\n", channel);
  69. }
  70. static ssize_t show_als_en(struct device *dev,
  71. struct device_attribute *attr, char *buf)
  72. {
  73. struct lm3533_bl *bl = dev_get_drvdata(dev);
  74. int ctrlbank = lm3533_bl_get_ctrlbank_id(bl);
  75. u8 val;
  76. u8 mask;
  77. bool enable;
  78. int ret;
  79. ret = lm3533_read(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, &val);
  80. if (ret)
  81. return ret;
  82. mask = 1 << (2 * ctrlbank);
  83. enable = val & mask;
  84. return scnprintf(buf, PAGE_SIZE, "%d\n", enable);
  85. }
  86. static ssize_t store_als_en(struct device *dev,
  87. struct device_attribute *attr,
  88. const char *buf, size_t len)
  89. {
  90. struct lm3533_bl *bl = dev_get_drvdata(dev);
  91. int ctrlbank = lm3533_bl_get_ctrlbank_id(bl);
  92. int enable;
  93. u8 val;
  94. u8 mask;
  95. int ret;
  96. if (kstrtoint(buf, 0, &enable))
  97. return -EINVAL;
  98. mask = 1 << (2 * ctrlbank);
  99. if (enable)
  100. val = mask;
  101. else
  102. val = 0;
  103. ret = lm3533_update(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, val,
  104. mask);
  105. if (ret)
  106. return ret;
  107. return len;
  108. }
  109. static ssize_t show_linear(struct device *dev,
  110. struct device_attribute *attr, char *buf)
  111. {
  112. struct lm3533_bl *bl = dev_get_drvdata(dev);
  113. u8 val;
  114. u8 mask;
  115. int linear;
  116. int ret;
  117. ret = lm3533_read(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, &val);
  118. if (ret)
  119. return ret;
  120. mask = 1 << (2 * lm3533_bl_get_ctrlbank_id(bl) + 1);
  121. if (val & mask)
  122. linear = 1;
  123. else
  124. linear = 0;
  125. return scnprintf(buf, PAGE_SIZE, "%x\n", linear);
  126. }
  127. static ssize_t store_linear(struct device *dev,
  128. struct device_attribute *attr,
  129. const char *buf, size_t len)
  130. {
  131. struct lm3533_bl *bl = dev_get_drvdata(dev);
  132. unsigned long linear;
  133. u8 mask;
  134. u8 val;
  135. int ret;
  136. if (kstrtoul(buf, 0, &linear))
  137. return -EINVAL;
  138. mask = 1 << (2 * lm3533_bl_get_ctrlbank_id(bl) + 1);
  139. if (linear)
  140. val = mask;
  141. else
  142. val = 0;
  143. ret = lm3533_update(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, val,
  144. mask);
  145. if (ret)
  146. return ret;
  147. return len;
  148. }
  149. static ssize_t show_pwm(struct device *dev,
  150. struct device_attribute *attr,
  151. char *buf)
  152. {
  153. struct lm3533_bl *bl = dev_get_drvdata(dev);
  154. u8 val;
  155. int ret;
  156. ret = lm3533_ctrlbank_get_pwm(&bl->cb, &val);
  157. if (ret)
  158. return ret;
  159. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  160. }
  161. static ssize_t store_pwm(struct device *dev,
  162. struct device_attribute *attr,
  163. const char *buf, size_t len)
  164. {
  165. struct lm3533_bl *bl = dev_get_drvdata(dev);
  166. u8 val;
  167. int ret;
  168. if (kstrtou8(buf, 0, &val))
  169. return -EINVAL;
  170. ret = lm3533_ctrlbank_set_pwm(&bl->cb, val);
  171. if (ret)
  172. return ret;
  173. return len;
  174. }
  175. static LM3533_ATTR_RO(als_channel);
  176. static LM3533_ATTR_RW(als_en);
  177. static LM3533_ATTR_RO(id);
  178. static LM3533_ATTR_RW(linear);
  179. static LM3533_ATTR_RW(pwm);
  180. static struct attribute *lm3533_bl_attributes[] = {
  181. &dev_attr_als_channel.attr,
  182. &dev_attr_als_en.attr,
  183. &dev_attr_id.attr,
  184. &dev_attr_linear.attr,
  185. &dev_attr_pwm.attr,
  186. NULL,
  187. };
  188. static umode_t lm3533_bl_attr_is_visible(struct kobject *kobj,
  189. struct attribute *attr, int n)
  190. {
  191. struct device *dev = container_of(kobj, struct device, kobj);
  192. struct lm3533_bl *bl = dev_get_drvdata(dev);
  193. umode_t mode = attr->mode;
  194. if (attr == &dev_attr_als_channel.attr ||
  195. attr == &dev_attr_als_en.attr) {
  196. if (!bl->lm3533->have_als)
  197. mode = 0;
  198. }
  199. return mode;
  200. };
  201. static struct attribute_group lm3533_bl_attribute_group = {
  202. .is_visible = lm3533_bl_attr_is_visible,
  203. .attrs = lm3533_bl_attributes
  204. };
  205. static int lm3533_bl_setup(struct lm3533_bl *bl,
  206. struct lm3533_bl_platform_data *pdata)
  207. {
  208. int ret;
  209. ret = lm3533_ctrlbank_set_max_current(&bl->cb, pdata->max_current);
  210. if (ret)
  211. return ret;
  212. return lm3533_ctrlbank_set_pwm(&bl->cb, pdata->pwm);
  213. }
  214. static int lm3533_bl_probe(struct platform_device *pdev)
  215. {
  216. struct lm3533 *lm3533;
  217. struct lm3533_bl_platform_data *pdata;
  218. struct lm3533_bl *bl;
  219. struct backlight_device *bd;
  220. struct backlight_properties props;
  221. int ret;
  222. dev_dbg(&pdev->dev, "%s\n", __func__);
  223. lm3533 = dev_get_drvdata(pdev->dev.parent);
  224. if (!lm3533)
  225. return -EINVAL;
  226. pdata = dev_get_platdata(&pdev->dev);
  227. if (!pdata) {
  228. dev_err(&pdev->dev, "no platform data\n");
  229. return -EINVAL;
  230. }
  231. if (pdev->id < 0 || pdev->id >= LM3533_HVCTRLBANK_COUNT) {
  232. dev_err(&pdev->dev, "illegal backlight id %d\n", pdev->id);
  233. return -EINVAL;
  234. }
  235. bl = devm_kzalloc(&pdev->dev, sizeof(*bl), GFP_KERNEL);
  236. if (!bl)
  237. return -ENOMEM;
  238. bl->lm3533 = lm3533;
  239. bl->id = pdev->id;
  240. bl->cb.lm3533 = lm3533;
  241. bl->cb.id = lm3533_bl_get_ctrlbank_id(bl);
  242. bl->cb.dev = NULL; /* until registered */
  243. memset(&props, 0, sizeof(props));
  244. props.type = BACKLIGHT_RAW;
  245. props.max_brightness = LM3533_BL_MAX_BRIGHTNESS;
  246. props.brightness = pdata->default_brightness;
  247. bd = devm_backlight_device_register(&pdev->dev, pdata->name,
  248. pdev->dev.parent, bl, &lm3533_bl_ops,
  249. &props);
  250. if (IS_ERR(bd)) {
  251. dev_err(&pdev->dev, "failed to register backlight device\n");
  252. return PTR_ERR(bd);
  253. }
  254. bl->bd = bd;
  255. bl->cb.dev = &bl->bd->dev;
  256. platform_set_drvdata(pdev, bl);
  257. ret = sysfs_create_group(&bd->dev.kobj, &lm3533_bl_attribute_group);
  258. if (ret < 0) {
  259. dev_err(&pdev->dev, "failed to create sysfs attributes\n");
  260. return ret;
  261. }
  262. backlight_update_status(bd);
  263. ret = lm3533_bl_setup(bl, pdata);
  264. if (ret)
  265. goto err_sysfs_remove;
  266. ret = lm3533_ctrlbank_enable(&bl->cb);
  267. if (ret)
  268. goto err_sysfs_remove;
  269. return 0;
  270. err_sysfs_remove:
  271. sysfs_remove_group(&bd->dev.kobj, &lm3533_bl_attribute_group);
  272. return ret;
  273. }
  274. static int lm3533_bl_remove(struct platform_device *pdev)
  275. {
  276. struct lm3533_bl *bl = platform_get_drvdata(pdev);
  277. struct backlight_device *bd = bl->bd;
  278. dev_dbg(&bd->dev, "%s\n", __func__);
  279. bd->props.power = FB_BLANK_POWERDOWN;
  280. bd->props.brightness = 0;
  281. lm3533_ctrlbank_disable(&bl->cb);
  282. sysfs_remove_group(&bd->dev.kobj, &lm3533_bl_attribute_group);
  283. return 0;
  284. }
  285. #ifdef CONFIG_PM_SLEEP
  286. static int lm3533_bl_suspend(struct device *dev)
  287. {
  288. struct lm3533_bl *bl = dev_get_drvdata(dev);
  289. dev_dbg(dev, "%s\n", __func__);
  290. return lm3533_ctrlbank_disable(&bl->cb);
  291. }
  292. static int lm3533_bl_resume(struct device *dev)
  293. {
  294. struct lm3533_bl *bl = dev_get_drvdata(dev);
  295. dev_dbg(dev, "%s\n", __func__);
  296. return lm3533_ctrlbank_enable(&bl->cb);
  297. }
  298. #endif
  299. static SIMPLE_DEV_PM_OPS(lm3533_bl_pm_ops, lm3533_bl_suspend, lm3533_bl_resume);
  300. static void lm3533_bl_shutdown(struct platform_device *pdev)
  301. {
  302. struct lm3533_bl *bl = platform_get_drvdata(pdev);
  303. dev_dbg(&pdev->dev, "%s\n", __func__);
  304. lm3533_ctrlbank_disable(&bl->cb);
  305. }
  306. static struct platform_driver lm3533_bl_driver = {
  307. .driver = {
  308. .name = "lm3533-backlight",
  309. .pm = &lm3533_bl_pm_ops,
  310. },
  311. .probe = lm3533_bl_probe,
  312. .remove = lm3533_bl_remove,
  313. .shutdown = lm3533_bl_shutdown,
  314. };
  315. module_platform_driver(lm3533_bl_driver);
  316. MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
  317. MODULE_DESCRIPTION("LM3533 Backlight driver");
  318. MODULE_LICENSE("GPL");
  319. MODULE_ALIAS("platform:lm3533-backlight");