leds-lm3530.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. #include <linux/module.h>
  21. #define LM3530_LED_DEV "lcd-backlight"
  22. #define LM3530_NAME "lm3530-led"
  23. #define LM3530_GEN_CONFIG 0x10
  24. #define LM3530_ALS_CONFIG 0x20
  25. #define LM3530_BRT_RAMP_RATE 0x30
  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 14
  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_ALS_ZB_MAX (4)
  62. #define LM3530_ALS_WINDOW_mV (1000)
  63. #define LM3530_ALS_OFFSET_mV (4)
  64. /* Zone Target Register defaults */
  65. #define LM3530_DEF_ZT_0 (0x7F)
  66. #define LM3530_DEF_ZT_1 (0x66)
  67. #define LM3530_DEF_ZT_2 (0x4C)
  68. #define LM3530_DEF_ZT_3 (0x33)
  69. #define LM3530_DEF_ZT_4 (0x19)
  70. /* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */
  71. #define MAX_BRIGHTNESS (127)
  72. struct lm3530_mode_map {
  73. const char *mode;
  74. enum lm3530_mode mode_val;
  75. };
  76. static struct lm3530_mode_map mode_map[] = {
  77. { "man", LM3530_BL_MODE_MANUAL },
  78. { "als", LM3530_BL_MODE_ALS },
  79. { "pwm", LM3530_BL_MODE_PWM },
  80. };
  81. /**
  82. * struct lm3530_data
  83. * @led_dev: led class device
  84. * @client: i2c client
  85. * @pdata: LM3530 platform data
  86. * @mode: mode of operation - manual, ALS, PWM
  87. * @regulator: regulator
  88. * @brighness: previous brightness value
  89. * @enable: regulator is enabled
  90. */
  91. struct lm3530_data {
  92. struct led_classdev led_dev;
  93. struct i2c_client *client;
  94. struct lm3530_platform_data *pdata;
  95. enum lm3530_mode mode;
  96. struct regulator *regulator;
  97. enum led_brightness brightness;
  98. bool enable;
  99. };
  100. /*
  101. * struct lm3530_als_data
  102. * @config : value of ALS configuration register
  103. * @imp_sel : value of ALS resistor select register
  104. * @zone : values of ALS ZB(Zone Boundary) registers
  105. */
  106. struct lm3530_als_data {
  107. u8 config;
  108. u8 imp_sel;
  109. u8 zones[LM3530_ALS_ZB_MAX];
  110. };
  111. static const u8 lm3530_reg[LM3530_REG_MAX] = {
  112. LM3530_GEN_CONFIG,
  113. LM3530_ALS_CONFIG,
  114. LM3530_BRT_RAMP_RATE,
  115. LM3530_ALS_IMP_SELECT,
  116. LM3530_BRT_CTRL_REG,
  117. LM3530_ALS_ZB0_REG,
  118. LM3530_ALS_ZB1_REG,
  119. LM3530_ALS_ZB2_REG,
  120. LM3530_ALS_ZB3_REG,
  121. LM3530_ALS_Z0T_REG,
  122. LM3530_ALS_Z1T_REG,
  123. LM3530_ALS_Z2T_REG,
  124. LM3530_ALS_Z3T_REG,
  125. LM3530_ALS_Z4T_REG,
  126. };
  127. static int lm3530_get_mode_from_str(const char *str)
  128. {
  129. int i;
  130. for (i = 0; i < ARRAY_SIZE(mode_map); i++)
  131. if (sysfs_streq(str, mode_map[i].mode))
  132. return mode_map[i].mode_val;
  133. return -EINVAL;
  134. }
  135. static void lm3530_als_configure(struct lm3530_platform_data *pdata,
  136. struct lm3530_als_data *als)
  137. {
  138. int i;
  139. u32 als_vmin, als_vmax, als_vstep;
  140. if (pdata->als_vmax == 0) {
  141. pdata->als_vmin = 0;
  142. pdata->als_vmax = LM3530_ALS_WINDOW_mV;
  143. }
  144. als_vmin = pdata->als_vmin;
  145. als_vmax = pdata->als_vmax;
  146. if ((als_vmax - als_vmin) > LM3530_ALS_WINDOW_mV)
  147. pdata->als_vmax = als_vmax = als_vmin + LM3530_ALS_WINDOW_mV;
  148. /* n zone boundary makes n+1 zones */
  149. als_vstep = (als_vmax - als_vmin) / (LM3530_ALS_ZB_MAX + 1);
  150. for (i = 0; i < LM3530_ALS_ZB_MAX; i++)
  151. als->zones[i] = (((als_vmin + LM3530_ALS_OFFSET_mV) +
  152. als_vstep + (i * als_vstep)) * LED_FULL) / 1000;
  153. als->config =
  154. (pdata->als_avrg_time << LM3530_ALS_AVG_TIME_SHIFT) |
  155. (LM3530_ENABLE_ALS) |
  156. (pdata->als_input_mode << LM3530_ALS_SEL_SHIFT);
  157. als->imp_sel =
  158. (pdata->als1_resistor_sel << LM3530_ALS1_IMP_SHIFT) |
  159. (pdata->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT);
  160. }
  161. static int lm3530_led_enable(struct lm3530_data *drvdata)
  162. {
  163. int ret;
  164. if (drvdata->enable)
  165. return 0;
  166. ret = regulator_enable(drvdata->regulator);
  167. if (ret) {
  168. dev_err(drvdata->led_dev.dev, "Failed to enable vin:%d\n", ret);
  169. return ret;
  170. }
  171. drvdata->enable = true;
  172. return 0;
  173. }
  174. static void lm3530_led_disable(struct lm3530_data *drvdata)
  175. {
  176. int ret;
  177. if (!drvdata->enable)
  178. return;
  179. ret = regulator_disable(drvdata->regulator);
  180. if (ret) {
  181. dev_err(drvdata->led_dev.dev, "Failed to disable vin:%d\n",
  182. ret);
  183. return;
  184. }
  185. drvdata->enable = false;
  186. }
  187. static int lm3530_init_registers(struct lm3530_data *drvdata)
  188. {
  189. int ret = 0;
  190. int i;
  191. u8 gen_config;
  192. u8 brt_ramp;
  193. u8 brightness;
  194. u8 reg_val[LM3530_REG_MAX];
  195. struct lm3530_platform_data *pdata = drvdata->pdata;
  196. struct i2c_client *client = drvdata->client;
  197. struct lm3530_pwm_data *pwm = &pdata->pwm_data;
  198. struct lm3530_als_data als;
  199. memset(&als, 0, sizeof(struct lm3530_als_data));
  200. gen_config = (pdata->brt_ramp_law << LM3530_RAMP_LAW_SHIFT) |
  201. ((pdata->max_current & 7) << LM3530_MAX_CURR_SHIFT);
  202. switch (drvdata->mode) {
  203. case LM3530_BL_MODE_MANUAL:
  204. gen_config |= LM3530_ENABLE_I2C;
  205. break;
  206. case LM3530_BL_MODE_ALS:
  207. gen_config |= LM3530_ENABLE_I2C;
  208. lm3530_als_configure(pdata, &als);
  209. break;
  210. case LM3530_BL_MODE_PWM:
  211. gen_config |= LM3530_ENABLE_PWM | LM3530_ENABLE_PWM_SIMPLE |
  212. (pdata->pwm_pol_hi << LM3530_PWM_POL_SHIFT);
  213. break;
  214. }
  215. brt_ramp = (pdata->brt_ramp_fall << LM3530_BRT_RAMP_FALL_SHIFT) |
  216. (pdata->brt_ramp_rise << LM3530_BRT_RAMP_RISE_SHIFT);
  217. if (drvdata->brightness)
  218. brightness = drvdata->brightness;
  219. else
  220. brightness = drvdata->brightness = pdata->brt_val;
  221. if (brightness > drvdata->led_dev.max_brightness)
  222. brightness = drvdata->led_dev.max_brightness;
  223. reg_val[0] = gen_config; /* LM3530_GEN_CONFIG */
  224. reg_val[1] = als.config; /* LM3530_ALS_CONFIG */
  225. reg_val[2] = brt_ramp; /* LM3530_BRT_RAMP_RATE */
  226. reg_val[3] = als.imp_sel; /* LM3530_ALS_IMP_SELECT */
  227. reg_val[4] = brightness; /* LM3530_BRT_CTRL_REG */
  228. reg_val[5] = als.zones[0]; /* LM3530_ALS_ZB0_REG */
  229. reg_val[6] = als.zones[1]; /* LM3530_ALS_ZB1_REG */
  230. reg_val[7] = als.zones[2]; /* LM3530_ALS_ZB2_REG */
  231. reg_val[8] = als.zones[3]; /* LM3530_ALS_ZB3_REG */
  232. reg_val[9] = LM3530_DEF_ZT_0; /* LM3530_ALS_Z0T_REG */
  233. reg_val[10] = LM3530_DEF_ZT_1; /* LM3530_ALS_Z1T_REG */
  234. reg_val[11] = LM3530_DEF_ZT_2; /* LM3530_ALS_Z2T_REG */
  235. reg_val[12] = LM3530_DEF_ZT_3; /* LM3530_ALS_Z3T_REG */
  236. reg_val[13] = LM3530_DEF_ZT_4; /* LM3530_ALS_Z4T_REG */
  237. ret = lm3530_led_enable(drvdata);
  238. if (ret)
  239. return ret;
  240. for (i = 0; i < LM3530_REG_MAX; i++) {
  241. /* do not update brightness register when pwm mode */
  242. if (lm3530_reg[i] == LM3530_BRT_CTRL_REG &&
  243. drvdata->mode == LM3530_BL_MODE_PWM) {
  244. if (pwm->pwm_set_intensity)
  245. pwm->pwm_set_intensity(reg_val[i],
  246. drvdata->led_dev.max_brightness);
  247. continue;
  248. }
  249. ret = i2c_smbus_write_byte_data(client,
  250. lm3530_reg[i], reg_val[i]);
  251. if (ret)
  252. break;
  253. }
  254. return ret;
  255. }
  256. static void lm3530_brightness_set(struct led_classdev *led_cdev,
  257. enum led_brightness brt_val)
  258. {
  259. int err;
  260. struct lm3530_data *drvdata =
  261. container_of(led_cdev, struct lm3530_data, led_dev);
  262. struct lm3530_platform_data *pdata = drvdata->pdata;
  263. struct lm3530_pwm_data *pwm = &pdata->pwm_data;
  264. u8 max_brightness = led_cdev->max_brightness;
  265. switch (drvdata->mode) {
  266. case LM3530_BL_MODE_MANUAL:
  267. if (!drvdata->enable) {
  268. err = lm3530_init_registers(drvdata);
  269. if (err) {
  270. dev_err(&drvdata->client->dev,
  271. "Register Init failed: %d\n", err);
  272. break;
  273. }
  274. }
  275. /* set the brightness in brightness control register*/
  276. err = i2c_smbus_write_byte_data(drvdata->client,
  277. LM3530_BRT_CTRL_REG, brt_val);
  278. if (err)
  279. dev_err(&drvdata->client->dev,
  280. "Unable to set brightness: %d\n", err);
  281. else
  282. drvdata->brightness = brt_val;
  283. if (brt_val == 0)
  284. lm3530_led_disable(drvdata);
  285. break;
  286. case LM3530_BL_MODE_ALS:
  287. break;
  288. case LM3530_BL_MODE_PWM:
  289. if (pwm->pwm_set_intensity)
  290. pwm->pwm_set_intensity(brt_val, max_brightness);
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. static ssize_t lm3530_mode_get(struct device *dev,
  297. struct device_attribute *attr, char *buf)
  298. {
  299. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  300. struct lm3530_data *drvdata;
  301. int i, len = 0;
  302. drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
  303. for (i = 0; i < ARRAY_SIZE(mode_map); i++)
  304. if (drvdata->mode == mode_map[i].mode_val)
  305. len += sprintf(buf + len, "[%s] ", mode_map[i].mode);
  306. else
  307. len += sprintf(buf + len, "%s ", mode_map[i].mode);
  308. len += sprintf(buf + len, "\n");
  309. return len;
  310. }
  311. static ssize_t lm3530_mode_set(struct device *dev, struct device_attribute
  312. *attr, const char *buf, size_t size)
  313. {
  314. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  315. struct lm3530_data *drvdata;
  316. struct lm3530_pwm_data *pwm;
  317. u8 max_brightness;
  318. int mode, err;
  319. drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
  320. pwm = &drvdata->pdata->pwm_data;
  321. max_brightness = led_cdev->max_brightness;
  322. mode = lm3530_get_mode_from_str(buf);
  323. if (mode < 0) {
  324. dev_err(dev, "Invalid mode\n");
  325. return mode;
  326. }
  327. drvdata->mode = mode;
  328. /* set pwm to low if unnecessary */
  329. if (mode != LM3530_BL_MODE_PWM && pwm->pwm_set_intensity)
  330. pwm->pwm_set_intensity(0, max_brightness);
  331. err = lm3530_init_registers(drvdata);
  332. if (err) {
  333. dev_err(dev, "Setting %s Mode failed :%d\n", buf, err);
  334. return err;
  335. }
  336. return sizeof(drvdata->mode);
  337. }
  338. static DEVICE_ATTR(mode, 0644, lm3530_mode_get, lm3530_mode_set);
  339. static struct attribute *lm3530_attrs[] = {
  340. &dev_attr_mode.attr,
  341. NULL
  342. };
  343. ATTRIBUTE_GROUPS(lm3530);
  344. static int lm3530_probe(struct i2c_client *client,
  345. const struct i2c_device_id *id)
  346. {
  347. struct lm3530_platform_data *pdata = dev_get_platdata(&client->dev);
  348. struct lm3530_data *drvdata;
  349. int err = 0;
  350. if (pdata == NULL) {
  351. dev_err(&client->dev, "platform data required\n");
  352. return -ENODEV;
  353. }
  354. /* BL mode */
  355. if (pdata->mode > LM3530_BL_MODE_PWM) {
  356. dev_err(&client->dev, "Illegal Mode request\n");
  357. return -EINVAL;
  358. }
  359. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  360. dev_err(&client->dev, "I2C_FUNC_I2C not supported\n");
  361. return -EIO;
  362. }
  363. drvdata = devm_kzalloc(&client->dev, sizeof(struct lm3530_data),
  364. GFP_KERNEL);
  365. if (drvdata == NULL)
  366. return -ENOMEM;
  367. drvdata->mode = pdata->mode;
  368. drvdata->client = client;
  369. drvdata->pdata = pdata;
  370. drvdata->brightness = LED_OFF;
  371. drvdata->enable = false;
  372. drvdata->led_dev.name = LM3530_LED_DEV;
  373. drvdata->led_dev.brightness_set = lm3530_brightness_set;
  374. drvdata->led_dev.max_brightness = MAX_BRIGHTNESS;
  375. drvdata->led_dev.groups = lm3530_groups;
  376. i2c_set_clientdata(client, drvdata);
  377. drvdata->regulator = devm_regulator_get(&client->dev, "vin");
  378. if (IS_ERR(drvdata->regulator)) {
  379. dev_err(&client->dev, "regulator get failed\n");
  380. err = PTR_ERR(drvdata->regulator);
  381. drvdata->regulator = NULL;
  382. return err;
  383. }
  384. if (drvdata->pdata->brt_val) {
  385. err = lm3530_init_registers(drvdata);
  386. if (err < 0) {
  387. dev_err(&client->dev,
  388. "Register Init failed: %d\n", err);
  389. return err;
  390. }
  391. }
  392. err = led_classdev_register(&client->dev, &drvdata->led_dev);
  393. if (err < 0) {
  394. dev_err(&client->dev, "Register led class failed: %d\n", err);
  395. return err;
  396. }
  397. return 0;
  398. }
  399. static int lm3530_remove(struct i2c_client *client)
  400. {
  401. struct lm3530_data *drvdata = i2c_get_clientdata(client);
  402. lm3530_led_disable(drvdata);
  403. led_classdev_unregister(&drvdata->led_dev);
  404. return 0;
  405. }
  406. static const struct i2c_device_id lm3530_id[] = {
  407. {LM3530_NAME, 0},
  408. {}
  409. };
  410. MODULE_DEVICE_TABLE(i2c, lm3530_id);
  411. static struct i2c_driver lm3530_i2c_driver = {
  412. .probe = lm3530_probe,
  413. .remove = lm3530_remove,
  414. .id_table = lm3530_id,
  415. .driver = {
  416. .name = LM3530_NAME,
  417. },
  418. };
  419. module_i2c_driver(lm3530_i2c_driver);
  420. MODULE_DESCRIPTION("Back Light driver for LM3530");
  421. MODULE_LICENSE("GPL v2");
  422. MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");