leds-lm3530.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (C) 2011 ST-Ericsson SA.
  3. * Copyright (C) 2009 Motorola, Inc.
  4. *
  5. * License Terms: GNU General Public License v2
  6. *
  7. * Simple driver for National Semiconductor LM3530 Backlight driver chip
  8. *
  9. * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>
  10. * based on leds-lm3530.c by Dan Murphy <D.Murphy@motorola.com>
  11. */
  12. #include <linux/i2c.h>
  13. #include <linux/leds.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/input.h>
  17. #include <linux/led-lm3530.h>
  18. #include <linux/types.h>
  19. #include <linux/regulator/consumer.h>
  20. #define LM3530_LED_DEV "lcd-backlight"
  21. #define LM3530_NAME "lm3530-led"
  22. #define LM3530_GEN_CONFIG 0x10
  23. #define LM3530_ALS_CONFIG 0x20
  24. #define LM3530_BRT_RAMP_RATE 0x30
  25. #define LM3530_ALS_ZONE_REG 0x40
  26. #define LM3530_ALS_IMP_SELECT 0x41
  27. #define LM3530_BRT_CTRL_REG 0xA0
  28. #define LM3530_ALS_ZB0_REG 0x60
  29. #define LM3530_ALS_ZB1_REG 0x61
  30. #define LM3530_ALS_ZB2_REG 0x62
  31. #define LM3530_ALS_ZB3_REG 0x63
  32. #define LM3530_ALS_Z0T_REG 0x70
  33. #define LM3530_ALS_Z1T_REG 0x71
  34. #define LM3530_ALS_Z2T_REG 0x72
  35. #define LM3530_ALS_Z3T_REG 0x73
  36. #define LM3530_ALS_Z4T_REG 0x74
  37. #define LM3530_REG_MAX 15
  38. /* General Control Register */
  39. #define LM3530_EN_I2C_SHIFT (0)
  40. #define LM3530_RAMP_LAW_SHIFT (1)
  41. #define LM3530_MAX_CURR_SHIFT (2)
  42. #define LM3530_EN_PWM_SHIFT (5)
  43. #define LM3530_PWM_POL_SHIFT (6)
  44. #define LM3530_EN_PWM_SIMPLE_SHIFT (7)
  45. #define LM3530_ENABLE_I2C (1 << LM3530_EN_I2C_SHIFT)
  46. #define LM3530_ENABLE_PWM (1 << LM3530_EN_PWM_SHIFT)
  47. #define LM3530_POL_LOW (1 << LM3530_PWM_POL_SHIFT)
  48. #define LM3530_ENABLE_PWM_SIMPLE (1 << LM3530_EN_PWM_SIMPLE_SHIFT)
  49. /* ALS Config Register Options */
  50. #define LM3530_ALS_AVG_TIME_SHIFT (0)
  51. #define LM3530_EN_ALS_SHIFT (3)
  52. #define LM3530_ALS_SEL_SHIFT (5)
  53. #define LM3530_ENABLE_ALS (3 << LM3530_EN_ALS_SHIFT)
  54. /* Brightness Ramp Rate Register */
  55. #define LM3530_BRT_RAMP_FALL_SHIFT (0)
  56. #define LM3530_BRT_RAMP_RISE_SHIFT (3)
  57. /* ALS Resistor Select */
  58. #define LM3530_ALS1_IMP_SHIFT (0)
  59. #define LM3530_ALS2_IMP_SHIFT (4)
  60. /* Zone Boundary Register defaults */
  61. #define LM3530_DEF_ZB_0 (0x33)
  62. #define LM3530_DEF_ZB_1 (0x66)
  63. #define LM3530_DEF_ZB_2 (0x99)
  64. #define LM3530_DEF_ZB_3 (0xCC)
  65. /* Zone Target Register defaults */
  66. #define LM3530_DEF_ZT_0 (0x19)
  67. #define LM3530_DEF_ZT_1 (0x33)
  68. #define LM3530_DEF_ZT_2 (0x4C)
  69. #define LM3530_DEF_ZT_3 (0x66)
  70. #define LM3530_DEF_ZT_4 (0x7F)
  71. struct lm3530_mode_map {
  72. const char *mode;
  73. enum lm3530_mode mode_val;
  74. };
  75. static struct lm3530_mode_map mode_map[] = {
  76. { "man", LM3530_BL_MODE_MANUAL },
  77. { "als", LM3530_BL_MODE_ALS },
  78. { "pwm", LM3530_BL_MODE_PWM },
  79. };
  80. /**
  81. * struct lm3530_data
  82. * @led_dev: led class device
  83. * @client: i2c client
  84. * @pdata: LM3530 platform data
  85. * @mode: mode of operation - manual, ALS, PWM
  86. * @regulator: regulator
  87. * @brighness: previous brightness value
  88. * @enable: regulator is enabled
  89. */
  90. struct lm3530_data {
  91. struct led_classdev led_dev;
  92. struct i2c_client *client;
  93. struct lm3530_platform_data *pdata;
  94. enum lm3530_mode mode;
  95. struct regulator *regulator;
  96. enum led_brightness brightness;
  97. bool enable;
  98. };
  99. static const u8 lm3530_reg[LM3530_REG_MAX] = {
  100. LM3530_GEN_CONFIG,
  101. LM3530_ALS_CONFIG,
  102. LM3530_BRT_RAMP_RATE,
  103. LM3530_ALS_ZONE_REG,
  104. LM3530_ALS_IMP_SELECT,
  105. LM3530_BRT_CTRL_REG,
  106. LM3530_ALS_ZB0_REG,
  107. LM3530_ALS_ZB1_REG,
  108. LM3530_ALS_ZB2_REG,
  109. LM3530_ALS_ZB3_REG,
  110. LM3530_ALS_Z0T_REG,
  111. LM3530_ALS_Z1T_REG,
  112. LM3530_ALS_Z2T_REG,
  113. LM3530_ALS_Z3T_REG,
  114. LM3530_ALS_Z4T_REG,
  115. };
  116. static int lm3530_get_mode_from_str(const char *str)
  117. {
  118. int i;
  119. for (i = 0; i < ARRAY_SIZE(mode_map); i++)
  120. if (sysfs_streq(str, mode_map[i].mode))
  121. return mode_map[i].mode_val;
  122. return -1;
  123. }
  124. static int lm3530_init_registers(struct lm3530_data *drvdata)
  125. {
  126. int ret = 0;
  127. int i;
  128. u8 gen_config;
  129. u8 als_config = 0;
  130. u8 brt_ramp;
  131. u8 als_imp_sel = 0;
  132. u8 brightness;
  133. u8 reg_val[LM3530_REG_MAX];
  134. struct lm3530_platform_data *pltfm = drvdata->pdata;
  135. struct i2c_client *client = drvdata->client;
  136. gen_config = (pltfm->brt_ramp_law << LM3530_RAMP_LAW_SHIFT) |
  137. ((pltfm->max_current & 7) << LM3530_MAX_CURR_SHIFT);
  138. if (drvdata->mode == LM3530_BL_MODE_MANUAL ||
  139. drvdata->mode == LM3530_BL_MODE_ALS)
  140. gen_config |= (LM3530_ENABLE_I2C);
  141. if (drvdata->mode == LM3530_BL_MODE_ALS) {
  142. als_config =
  143. (pltfm->als_avrg_time << LM3530_ALS_AVG_TIME_SHIFT) |
  144. (LM3530_ENABLE_ALS) |
  145. (pltfm->als_input_mode << LM3530_ALS_SEL_SHIFT);
  146. als_imp_sel =
  147. (pltfm->als1_resistor_sel << LM3530_ALS1_IMP_SHIFT) |
  148. (pltfm->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT);
  149. }
  150. if (drvdata->mode == LM3530_BL_MODE_PWM)
  151. gen_config |= (LM3530_ENABLE_PWM) |
  152. (pltfm->pwm_pol_hi << LM3530_PWM_POL_SHIFT) |
  153. (LM3530_ENABLE_PWM_SIMPLE);
  154. brt_ramp = (pltfm->brt_ramp_fall << LM3530_BRT_RAMP_FALL_SHIFT) |
  155. (pltfm->brt_ramp_rise << LM3530_BRT_RAMP_RISE_SHIFT);
  156. if (drvdata->brightness)
  157. brightness = drvdata->brightness;
  158. else
  159. brightness = drvdata->brightness = pltfm->brt_val;
  160. reg_val[0] = gen_config; /* LM3530_GEN_CONFIG */
  161. reg_val[1] = als_config; /* LM3530_ALS_CONFIG */
  162. reg_val[2] = brt_ramp; /* LM3530_BRT_RAMP_RATE */
  163. reg_val[3] = 0x00; /* LM3530_ALS_ZONE_REG */
  164. reg_val[4] = als_imp_sel; /* LM3530_ALS_IMP_SELECT */
  165. reg_val[5] = brightness; /* LM3530_BRT_CTRL_REG */
  166. reg_val[6] = LM3530_DEF_ZB_0; /* LM3530_ALS_ZB0_REG */
  167. reg_val[7] = LM3530_DEF_ZB_1; /* LM3530_ALS_ZB1_REG */
  168. reg_val[8] = LM3530_DEF_ZB_2; /* LM3530_ALS_ZB2_REG */
  169. reg_val[9] = LM3530_DEF_ZB_3; /* LM3530_ALS_ZB3_REG */
  170. reg_val[10] = LM3530_DEF_ZT_0; /* LM3530_ALS_Z0T_REG */
  171. reg_val[11] = LM3530_DEF_ZT_1; /* LM3530_ALS_Z1T_REG */
  172. reg_val[12] = LM3530_DEF_ZT_2; /* LM3530_ALS_Z2T_REG */
  173. reg_val[13] = LM3530_DEF_ZT_3; /* LM3530_ALS_Z3T_REG */
  174. reg_val[14] = LM3530_DEF_ZT_4; /* LM3530_ALS_Z4T_REG */
  175. if (!drvdata->enable) {
  176. ret = regulator_enable(drvdata->regulator);
  177. if (ret) {
  178. dev_err(&drvdata->client->dev,
  179. "Enable regulator failed\n");
  180. return ret;
  181. }
  182. drvdata->enable = true;
  183. }
  184. for (i = 0; i < LM3530_REG_MAX; i++) {
  185. ret = i2c_smbus_write_byte_data(client,
  186. lm3530_reg[i], reg_val[i]);
  187. if (ret)
  188. break;
  189. }
  190. return ret;
  191. }
  192. static void lm3530_brightness_set(struct led_classdev *led_cdev,
  193. enum led_brightness brt_val)
  194. {
  195. int err;
  196. struct lm3530_data *drvdata =
  197. container_of(led_cdev, struct lm3530_data, led_dev);
  198. switch (drvdata->mode) {
  199. case LM3530_BL_MODE_MANUAL:
  200. if (!drvdata->enable) {
  201. err = lm3530_init_registers(drvdata);
  202. if (err) {
  203. dev_err(&drvdata->client->dev,
  204. "Register Init failed: %d\n", err);
  205. break;
  206. }
  207. }
  208. /* set the brightness in brightness control register*/
  209. err = i2c_smbus_write_byte_data(drvdata->client,
  210. LM3530_BRT_CTRL_REG, brt_val / 2);
  211. if (err)
  212. dev_err(&drvdata->client->dev,
  213. "Unable to set brightness: %d\n", err);
  214. else
  215. drvdata->brightness = brt_val / 2;
  216. if (brt_val == 0) {
  217. err = regulator_disable(drvdata->regulator);
  218. if (err)
  219. dev_err(&drvdata->client->dev,
  220. "Disable regulator failed\n");
  221. drvdata->enable = false;
  222. }
  223. break;
  224. case LM3530_BL_MODE_ALS:
  225. break;
  226. case LM3530_BL_MODE_PWM:
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. static ssize_t lm3530_mode_set(struct device *dev, struct device_attribute
  233. *attr, const char *buf, size_t size)
  234. {
  235. int err;
  236. struct i2c_client *client = container_of(
  237. dev->parent, struct i2c_client, dev);
  238. struct lm3530_data *drvdata = i2c_get_clientdata(client);
  239. int mode;
  240. mode = lm3530_get_mode_from_str(buf);
  241. if (mode < 0) {
  242. dev_err(dev, "Invalid mode\n");
  243. return -EINVAL;
  244. }
  245. if (mode == LM3530_BL_MODE_MANUAL)
  246. drvdata->mode = LM3530_BL_MODE_MANUAL;
  247. else if (mode == LM3530_BL_MODE_ALS)
  248. drvdata->mode = LM3530_BL_MODE_ALS;
  249. else if (mode == LM3530_BL_MODE_PWM) {
  250. dev_err(dev, "PWM mode not supported\n");
  251. return -EINVAL;
  252. }
  253. err = lm3530_init_registers(drvdata);
  254. if (err) {
  255. dev_err(dev, "Setting %s Mode failed :%d\n", buf, err);
  256. return err;
  257. }
  258. return sizeof(drvdata->mode);
  259. }
  260. static DEVICE_ATTR(mode, 0644, NULL, lm3530_mode_set);
  261. static int __devinit lm3530_probe(struct i2c_client *client,
  262. const struct i2c_device_id *id)
  263. {
  264. struct lm3530_platform_data *pdata = client->dev.platform_data;
  265. struct lm3530_data *drvdata;
  266. int err = 0;
  267. if (pdata == NULL) {
  268. dev_err(&client->dev, "platform data required\n");
  269. err = -ENODEV;
  270. goto err_out;
  271. }
  272. /* BL mode */
  273. if (pdata->mode > LM3530_BL_MODE_PWM) {
  274. dev_err(&client->dev, "Illegal Mode request\n");
  275. err = -EINVAL;
  276. goto err_out;
  277. }
  278. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  279. dev_err(&client->dev, "I2C_FUNC_I2C not supported\n");
  280. err = -EIO;
  281. goto err_out;
  282. }
  283. drvdata = kzalloc(sizeof(struct lm3530_data), GFP_KERNEL);
  284. if (drvdata == NULL) {
  285. err = -ENOMEM;
  286. goto err_out;
  287. }
  288. drvdata->mode = pdata->mode;
  289. drvdata->client = client;
  290. drvdata->pdata = pdata;
  291. drvdata->brightness = LED_OFF;
  292. drvdata->enable = false;
  293. drvdata->led_dev.name = LM3530_LED_DEV;
  294. drvdata->led_dev.brightness_set = lm3530_brightness_set;
  295. i2c_set_clientdata(client, drvdata);
  296. drvdata->regulator = regulator_get(&client->dev, "vin");
  297. if (IS_ERR(drvdata->regulator)) {
  298. dev_err(&client->dev, "regulator get failed\n");
  299. err = PTR_ERR(drvdata->regulator);
  300. drvdata->regulator = NULL;
  301. goto err_regulator_get;
  302. }
  303. if (drvdata->pdata->brt_val) {
  304. err = lm3530_init_registers(drvdata);
  305. if (err < 0) {
  306. dev_err(&client->dev,
  307. "Register Init failed: %d\n", err);
  308. err = -ENODEV;
  309. goto err_reg_init;
  310. }
  311. }
  312. err = led_classdev_register(&client->dev, &drvdata->led_dev);
  313. if (err < 0) {
  314. dev_err(&client->dev, "Register led class failed: %d\n", err);
  315. err = -ENODEV;
  316. goto err_class_register;
  317. }
  318. err = device_create_file(drvdata->led_dev.dev, &dev_attr_mode);
  319. if (err < 0) {
  320. dev_err(&client->dev, "File device creation failed: %d\n", err);
  321. err = -ENODEV;
  322. goto err_create_file;
  323. }
  324. return 0;
  325. err_create_file:
  326. led_classdev_unregister(&drvdata->led_dev);
  327. err_class_register:
  328. err_reg_init:
  329. regulator_put(drvdata->regulator);
  330. err_regulator_get:
  331. i2c_set_clientdata(client, NULL);
  332. kfree(drvdata);
  333. err_out:
  334. return err;
  335. }
  336. static int __devexit lm3530_remove(struct i2c_client *client)
  337. {
  338. struct lm3530_data *drvdata = i2c_get_clientdata(client);
  339. device_remove_file(drvdata->led_dev.dev, &dev_attr_mode);
  340. if (drvdata->enable)
  341. regulator_disable(drvdata->regulator);
  342. regulator_put(drvdata->regulator);
  343. led_classdev_unregister(&drvdata->led_dev);
  344. kfree(drvdata);
  345. return 0;
  346. }
  347. static const struct i2c_device_id lm3530_id[] = {
  348. {LM3530_NAME, 0},
  349. {}
  350. };
  351. MODULE_DEVICE_TABLE(i2c, lm3530_id);
  352. static struct i2c_driver lm3530_i2c_driver = {
  353. .probe = lm3530_probe,
  354. .remove = lm3530_remove,
  355. .id_table = lm3530_id,
  356. .driver = {
  357. .name = LM3530_NAME,
  358. .owner = THIS_MODULE,
  359. },
  360. };
  361. static int __init lm3530_init(void)
  362. {
  363. return i2c_add_driver(&lm3530_i2c_driver);
  364. }
  365. static void __exit lm3530_exit(void)
  366. {
  367. i2c_del_driver(&lm3530_i2c_driver);
  368. }
  369. module_init(lm3530_init);
  370. module_exit(lm3530_exit);
  371. MODULE_DESCRIPTION("Back Light driver for LM3530");
  372. MODULE_LICENSE("GPL v2");
  373. MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");