leds-pm8xxx.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/leds.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/err.h>
  21. #include <linux/mfd/pm8xxx/core.h>
  22. #include <linux/mfd/pm8xxx/pwm.h>
  23. #include <linux/leds-pm8xxx.h>
  24. #define SSBI_REG_ADDR_DRV_KEYPAD 0x48
  25. #define PM8XXX_DRV_KEYPAD_BL_MASK 0xf0
  26. #define PM8XXX_DRV_KEYPAD_BL_SHIFT 0x04
  27. #define SSBI_REG_ADDR_FLASH_DRV0 0x49
  28. #define PM8XXX_DRV_FLASH_MASK 0xf0
  29. #define PM8XXX_DRV_FLASH_SHIFT 0x04
  30. #define SSBI_REG_ADDR_FLASH_DRV1 0xFB
  31. #define SSBI_REG_ADDR_LED_CTRL_BASE 0x131
  32. #define SSBI_REG_ADDR_LED_CTRL(n) (SSBI_REG_ADDR_LED_CTRL_BASE + (n))
  33. #define PM8XXX_DRV_LED_CTRL_MASK 0xf8
  34. #define PM8XXX_DRV_LED_CTRL_SHIFT 0x03
  35. #define SSBI_REG_ADDR_WLED_CTRL_BASE 0x25A
  36. #define SSBI_REG_ADDR_WLED_CTRL(n) (SSBI_REG_ADDR_WLED_CTRL_BASE + (n) - 1)
  37. /* wled control registers */
  38. #define WLED_MOD_CTRL_REG SSBI_REG_ADDR_WLED_CTRL(1)
  39. #define WLED_MAX_CURR_CFG_REG(n) SSBI_REG_ADDR_WLED_CTRL(n + 2)
  40. #define WLED_BRIGHTNESS_CNTL_REG1(n) SSBI_REG_ADDR_WLED_CTRL((2 * n) + 5)
  41. #define WLED_BRIGHTNESS_CNTL_REG2(n) SSBI_REG_ADDR_WLED_CTRL((2 * n) + 6)
  42. #define WLED_SYNC_REG SSBI_REG_ADDR_WLED_CTRL(11)
  43. #define WLED_OVP_CFG_REG SSBI_REG_ADDR_WLED_CTRL(13)
  44. #define WLED_BOOST_CFG_REG SSBI_REG_ADDR_WLED_CTRL(14)
  45. #define WLED_HIGH_POLE_CAP_REG SSBI_REG_ADDR_WLED_CTRL(16)
  46. #define WLED_STRING_ONE 0 /* Rightmost string */
  47. #define WLED_STRING_TWO 1 /* Middle string */
  48. #define WLED_STRING_THREE 2 /* Leftmost string */
  49. #define WLED_STRINGS 0x03
  50. #define WLED_OVP_VAL_MASK 0x30
  51. #define WLED_OVP_VAL_BIT_SHFT 0x04
  52. #define WLED_BOOST_LIMIT_MASK 0xE0
  53. #define WLED_BOOST_LIMIT_BIT_SHFT 0x05
  54. #define WLED_BOOST_OFF 0x00
  55. #define WLED_EN_MASK 0x01
  56. #define WLED_CP_SELECT_MAX 0x03
  57. #define WLED_CP_SELECT_MASK 0x03
  58. #define WLED_DIG_MOD_GEN_MASK 0x70
  59. #define WLED_CS_OUT_MASK 0x0E
  60. #define WLED_CTL_DLY_STEP 200
  61. #define WLED_CTL_DLY_MAX 1400
  62. #define WLED_CTL_DLY_MASK 0xE0
  63. #define WLED_CTL_DLY_BIT_SHFT 0x05
  64. #define WLED_MAX_CURR 25
  65. #define WLED_MAX_CURR_MASK 0x1F
  66. #define WLED_BRIGHTNESS_MSB_MASK 0x0F
  67. #define WLED_OP_FDBCK_MASK 0x1C
  68. #define WLED_OP_FDBCK_BIT_SHFT 0x02
  69. #define WLED_MAX_LEVEL 255
  70. #define WLED_8_BIT_MASK 0xFF
  71. #define WLED_8_BIT_SHFT 0x08
  72. #define WLED_MAX_DUTY_CYCLE 0xFFF
  73. #define WLED_SYNC_VAL 0x07
  74. #define WLED_SYNC_RESET_VAL 0x00
  75. #define WLED_SYNC_MASK 0xF8
  76. #define ONE_WLED_STRING 1
  77. #define TWO_WLED_STRINGS 2
  78. #define THREE_WLED_STRINGS 3
  79. #define WLED_CABC_SHIFT 3
  80. #define SSBI_REG_ADDR_RGB_CNTL1 0x12D
  81. #define SSBI_REG_ADDR_RGB_CNTL2 0x12E
  82. #define PM8XXX_DRV_RGB_RED_LED BIT(2)
  83. #define PM8XXX_DRV_RGB_GREEN_LED BIT(1)
  84. #define PM8XXX_DRV_RGB_BLUE_LED BIT(0)
  85. #define MAX_FLASH_LED_CURRENT 300
  86. #define MAX_LC_LED_CURRENT 40
  87. #define MAX_KP_BL_LED_CURRENT 300
  88. #define PM8XXX_ID_LED_CURRENT_FACTOR 2 /* Iout = x * 2mA */
  89. #define PM8XXX_ID_FLASH_CURRENT_FACTOR 20 /* Iout = x * 20mA */
  90. #define PM8XXX_FLASH_MODE_DBUS1 1
  91. #define PM8XXX_FLASH_MODE_DBUS2 2
  92. #define PM8XXX_FLASH_MODE_PWM 3
  93. #define MAX_LC_LED_BRIGHTNESS 20
  94. #define MAX_FLASH_BRIGHTNESS 15
  95. #define MAX_KB_LED_BRIGHTNESS 15
  96. #define PM8XXX_LED_OFFSET(id) ((id) - PM8XXX_ID_LED_0)
  97. #define PM8XXX_LED_PWM_FLAGS (PM_PWM_LUT_LOOP | PM_PWM_LUT_RAMP_UP)
  98. #define LED_MAP(_version, _kb, _led0, _led1, _led2, _flash_led0, _flash_led1, \
  99. _wled, _rgb_led_red, _rgb_led_green, _rgb_led_blue)\
  100. {\
  101. .version = _version,\
  102. .supported = _kb << PM8XXX_ID_LED_KB_LIGHT | \
  103. _led0 << PM8XXX_ID_LED_0 | _led1 << PM8XXX_ID_LED_1 | \
  104. _led2 << PM8XXX_ID_LED_2 | \
  105. _flash_led0 << PM8XXX_ID_FLASH_LED_0 | \
  106. _flash_led1 << PM8XXX_ID_FLASH_LED_1 | \
  107. _wled << PM8XXX_ID_WLED | \
  108. _rgb_led_red << PM8XXX_ID_RGB_LED_RED | \
  109. _rgb_led_green << PM8XXX_ID_RGB_LED_GREEN | \
  110. _rgb_led_blue << PM8XXX_ID_RGB_LED_BLUE, \
  111. }
  112. #define PM8XXX_PWM_CURRENT_4MA 4
  113. #define PM8XXX_PWM_CURRENT_8MA 8
  114. #define PM8XXX_PWM_CURRENT_12MA 12
  115. /**
  116. * supported_leds - leds supported for each PMIC version
  117. * @version - version of PMIC
  118. * @supported - which leds are supported on version
  119. */
  120. struct supported_leds {
  121. enum pm8xxx_version version;
  122. u32 supported;
  123. };
  124. static const struct supported_leds led_map[] = {
  125. LED_MAP(PM8XXX_VERSION_8058, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0),
  126. LED_MAP(PM8XXX_VERSION_8921, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0),
  127. LED_MAP(PM8XXX_VERSION_8018, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  128. LED_MAP(PM8XXX_VERSION_8922, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1),
  129. LED_MAP(PM8XXX_VERSION_8038, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1),
  130. };
  131. /**
  132. * struct pm8xxx_led_data - internal led data structure
  133. * @led_classdev - led class device
  134. * @id - led index
  135. * @work - workqueue for led
  136. * @lock - to protect the transactions
  137. * @reg - cached value of led register
  138. * @pwm_dev - pointer to PWM device if LED is driven using PWM
  139. * @pwm_channel - PWM channel ID
  140. * @pwm_period_us - PWM period in micro seconds
  141. * @pwm_duty_cycles - struct that describes PWM duty cycles info
  142. */
  143. struct pm8xxx_led_data {
  144. struct led_classdev cdev;
  145. int id;
  146. u8 reg;
  147. u8 wled_mod_ctrl_val;
  148. struct device *dev;
  149. struct work_struct work;
  150. struct mutex lock;
  151. struct pwm_device *pwm_dev;
  152. int pwm_channel;
  153. u32 pwm_period_us;
  154. struct pm8xxx_pwm_duty_cycles *pwm_duty_cycles;
  155. struct wled_config_data *wled_cfg;
  156. int max_current;
  157. };
  158. static void led_kp_set(struct pm8xxx_led_data *led, enum led_brightness value)
  159. {
  160. int rc;
  161. u8 level;
  162. level = (value << PM8XXX_DRV_KEYPAD_BL_SHIFT) &
  163. PM8XXX_DRV_KEYPAD_BL_MASK;
  164. led->reg &= ~PM8XXX_DRV_KEYPAD_BL_MASK;
  165. led->reg |= level;
  166. rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_DRV_KEYPAD,
  167. led->reg);
  168. if (rc < 0)
  169. dev_err(led->cdev.dev,
  170. "can't set keypad backlight level rc=%d\n", rc);
  171. }
  172. static void led_lc_set(struct pm8xxx_led_data *led, enum led_brightness value)
  173. {
  174. int rc, offset;
  175. u8 level;
  176. level = (value << PM8XXX_DRV_LED_CTRL_SHIFT) &
  177. PM8XXX_DRV_LED_CTRL_MASK;
  178. offset = PM8XXX_LED_OFFSET(led->id);
  179. led->reg &= ~PM8XXX_DRV_LED_CTRL_MASK;
  180. led->reg |= level;
  181. rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_LED_CTRL(offset),
  182. led->reg);
  183. if (rc)
  184. dev_err(led->cdev.dev, "can't set (%d) led value rc=%d\n",
  185. led->id, rc);
  186. }
  187. static void
  188. led_flash_set(struct pm8xxx_led_data *led, enum led_brightness value)
  189. {
  190. int rc;
  191. u8 level;
  192. u16 reg_addr;
  193. level = (value << PM8XXX_DRV_FLASH_SHIFT) &
  194. PM8XXX_DRV_FLASH_MASK;
  195. led->reg &= ~PM8XXX_DRV_FLASH_MASK;
  196. led->reg |= level;
  197. if (led->id == PM8XXX_ID_FLASH_LED_0)
  198. reg_addr = SSBI_REG_ADDR_FLASH_DRV0;
  199. else
  200. reg_addr = SSBI_REG_ADDR_FLASH_DRV1;
  201. rc = pm8xxx_writeb(led->dev->parent, reg_addr, led->reg);
  202. if (rc < 0)
  203. dev_err(led->cdev.dev, "can't set flash led%d level rc=%d\n",
  204. led->id, rc);
  205. }
  206. static int
  207. led_wled_set(struct pm8xxx_led_data *led, enum led_brightness value)
  208. {
  209. int rc, duty;
  210. u8 val, i;
  211. if (value > WLED_MAX_LEVEL)
  212. value = WLED_MAX_LEVEL;
  213. if (value == 0) {
  214. rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG,
  215. WLED_BOOST_OFF);
  216. if (rc) {
  217. dev_err(led->dev->parent, "can't write wled ctrl config"
  218. " register rc=%d\n", rc);
  219. return rc;
  220. }
  221. } else {
  222. rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG,
  223. led->wled_mod_ctrl_val);
  224. if (rc) {
  225. dev_err(led->dev->parent, "can't write wled ctrl config"
  226. " register rc=%d\n", rc);
  227. return rc;
  228. }
  229. }
  230. duty = (WLED_MAX_DUTY_CYCLE * value) / WLED_MAX_LEVEL;
  231. /* program brightness control registers */
  232. for (i = 0; i < WLED_STRINGS; i++) {
  233. if (led->wled_cfg->strings && (1 << i)) {
  234. rc = pm8xxx_readb(led->dev->parent,
  235. WLED_BRIGHTNESS_CNTL_REG1(i), &val);
  236. if (rc) {
  237. dev_err(led->dev->parent,
  238. "can't read wled brightnes ctrl"
  239. " register1 rc=%d\n", rc);
  240. return rc;
  241. }
  242. val = (val & ~WLED_MAX_CURR_MASK) |
  243. (duty >> WLED_8_BIT_SHFT);
  244. rc = pm8xxx_writeb(led->dev->parent,
  245. WLED_BRIGHTNESS_CNTL_REG1(i), val);
  246. if (rc) {
  247. dev_err(led->dev->parent,
  248. "can't write wled brightness ctrl"
  249. " register1 rc=%d\n", rc);
  250. return rc;
  251. }
  252. val = duty & WLED_8_BIT_MASK;
  253. rc = pm8xxx_writeb(led->dev->parent,
  254. WLED_BRIGHTNESS_CNTL_REG2(i), val);
  255. if (rc) {
  256. dev_err(led->dev->parent,
  257. "can't write wled brightness ctrl"
  258. " register2 rc=%d\n", rc);
  259. return rc;
  260. }
  261. } else
  262. continue;
  263. }
  264. rc = pm8xxx_readb(led->dev->parent, WLED_SYNC_REG, &val);
  265. if (rc) {
  266. dev_err(led->dev->parent,
  267. "can't read wled sync register rc=%d\n", rc);
  268. return rc;
  269. }
  270. /* sync */
  271. val &= WLED_SYNC_MASK;
  272. val |= WLED_SYNC_VAL;
  273. rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val);
  274. if (rc) {
  275. dev_err(led->dev->parent,
  276. "can't read wled sync register rc=%d\n", rc);
  277. return rc;
  278. }
  279. val &= WLED_SYNC_MASK;
  280. val |= WLED_SYNC_RESET_VAL;
  281. rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val);
  282. if (rc) {
  283. dev_err(led->dev->parent,
  284. "can't read wled sync register rc=%d\n", rc);
  285. return rc;
  286. }
  287. return 0;
  288. }
  289. static void wled_dump_regs(struct pm8xxx_led_data *led)
  290. {
  291. int i;
  292. u8 val;
  293. for (i = 1; i < 17; i++) {
  294. pm8xxx_readb(led->dev->parent,
  295. SSBI_REG_ADDR_WLED_CTRL(i), &val);
  296. pr_debug("WLED_CTRL_%d = 0x%x\n", i, val);
  297. }
  298. }
  299. static void
  300. led_rgb_write(struct pm8xxx_led_data *led, u16 addr, enum led_brightness value)
  301. {
  302. int rc;
  303. u8 val, mask;
  304. if (led->id != PM8XXX_ID_RGB_LED_BLUE &&
  305. led->id != PM8XXX_ID_RGB_LED_RED &&
  306. led->id != PM8XXX_ID_RGB_LED_GREEN)
  307. return;
  308. rc = pm8xxx_readb(led->dev->parent, addr, &val);
  309. if (rc) {
  310. dev_err(led->cdev.dev, "can't read rgb ctrl register rc=%d\n",
  311. rc);
  312. return;
  313. }
  314. switch (led->id) {
  315. case PM8XXX_ID_RGB_LED_RED:
  316. mask = PM8XXX_DRV_RGB_RED_LED;
  317. break;
  318. case PM8XXX_ID_RGB_LED_GREEN:
  319. mask = PM8XXX_DRV_RGB_GREEN_LED;
  320. break;
  321. case PM8XXX_ID_RGB_LED_BLUE:
  322. mask = PM8XXX_DRV_RGB_BLUE_LED;
  323. break;
  324. default:
  325. return;
  326. }
  327. if (value)
  328. val |= mask;
  329. else
  330. val &= ~mask;
  331. rc = pm8xxx_writeb(led->dev->parent, addr, val);
  332. if (rc < 0)
  333. dev_err(led->cdev.dev, "can't set rgb led %d level rc=%d\n",
  334. led->id, rc);
  335. }
  336. static void
  337. led_rgb_set(struct pm8xxx_led_data *led, enum led_brightness value)
  338. {
  339. if (value) {
  340. led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, value);
  341. led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL2, value);
  342. } else {
  343. led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL2, value);
  344. led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, value);
  345. }
  346. }
  347. static int pm8xxx_led_pwm_work(struct pm8xxx_led_data *led)
  348. {
  349. int duty_us;
  350. int rc = 0;
  351. if (led->pwm_duty_cycles == NULL) {
  352. duty_us = (led->pwm_period_us * led->cdev.brightness) /
  353. LED_FULL;
  354. rc = pwm_config(led->pwm_dev, duty_us, led->pwm_period_us);
  355. if (led->cdev.brightness) {
  356. led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1,
  357. led->cdev.brightness);
  358. rc = pwm_enable(led->pwm_dev);
  359. } else {
  360. pwm_disable(led->pwm_dev);
  361. led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1,
  362. led->cdev.brightness);
  363. }
  364. } else {
  365. if (led->cdev.brightness)
  366. led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1,
  367. led->cdev.brightness);
  368. rc = pm8xxx_pwm_lut_enable(led->pwm_dev, led->cdev.brightness);
  369. if (!led->cdev.brightness)
  370. led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1,
  371. led->cdev.brightness);
  372. }
  373. return rc;
  374. }
  375. static void __pm8xxx_led_work(struct pm8xxx_led_data *led,
  376. enum led_brightness level)
  377. {
  378. int rc;
  379. mutex_lock(&led->lock);
  380. switch (led->id) {
  381. case PM8XXX_ID_LED_KB_LIGHT:
  382. led_kp_set(led, level);
  383. break;
  384. case PM8XXX_ID_LED_0:
  385. case PM8XXX_ID_LED_1:
  386. case PM8XXX_ID_LED_2:
  387. led_lc_set(led, level);
  388. break;
  389. case PM8XXX_ID_FLASH_LED_0:
  390. case PM8XXX_ID_FLASH_LED_1:
  391. led_flash_set(led, level);
  392. break;
  393. case PM8XXX_ID_WLED:
  394. rc = led_wled_set(led, level);
  395. if (rc < 0)
  396. pr_err("wled brightness set failed %d\n", rc);
  397. break;
  398. case PM8XXX_ID_RGB_LED_RED:
  399. case PM8XXX_ID_RGB_LED_GREEN:
  400. case PM8XXX_ID_RGB_LED_BLUE:
  401. led_rgb_set(led, level);
  402. break;
  403. default:
  404. dev_err(led->cdev.dev, "unknown led id %d", led->id);
  405. break;
  406. }
  407. mutex_unlock(&led->lock);
  408. }
  409. static void pm8xxx_led_work(struct work_struct *work)
  410. {
  411. int rc;
  412. struct pm8xxx_led_data *led = container_of(work,
  413. struct pm8xxx_led_data, work);
  414. if (led->pwm_dev == NULL) {
  415. __pm8xxx_led_work(led, led->cdev.brightness);
  416. } else {
  417. rc = pm8xxx_led_pwm_work(led);
  418. if (rc)
  419. pr_err("could not configure PWM mode for LED:%d\n",
  420. led->id);
  421. }
  422. }
  423. static void pm8xxx_led_set(struct led_classdev *led_cdev,
  424. enum led_brightness value)
  425. {
  426. struct pm8xxx_led_data *led;
  427. led = container_of(led_cdev, struct pm8xxx_led_data, cdev);
  428. if (value < LED_OFF || value > led->cdev.max_brightness) {
  429. dev_err(led->cdev.dev, "Invalid brightness value exceeds");
  430. return;
  431. }
  432. led->cdev.brightness = value;
  433. schedule_work(&led->work);
  434. }
  435. static int pm8xxx_set_led_mode_and_max_brightness(struct pm8xxx_led_data *led,
  436. enum pm8xxx_led_modes led_mode, int max_current)
  437. {
  438. switch (led->id) {
  439. case PM8XXX_ID_LED_0:
  440. case PM8XXX_ID_LED_1:
  441. case PM8XXX_ID_LED_2:
  442. led->cdev.max_brightness = max_current /
  443. PM8XXX_ID_LED_CURRENT_FACTOR;
  444. if (led->cdev.max_brightness > MAX_LC_LED_BRIGHTNESS)
  445. led->cdev.max_brightness = MAX_LC_LED_BRIGHTNESS;
  446. led->reg = led_mode;
  447. break;
  448. case PM8XXX_ID_LED_KB_LIGHT:
  449. case PM8XXX_ID_FLASH_LED_0:
  450. case PM8XXX_ID_FLASH_LED_1:
  451. led->cdev.max_brightness = max_current /
  452. PM8XXX_ID_FLASH_CURRENT_FACTOR;
  453. if (led->cdev.max_brightness > MAX_FLASH_BRIGHTNESS)
  454. led->cdev.max_brightness = MAX_FLASH_BRIGHTNESS;
  455. switch (led_mode) {
  456. case PM8XXX_LED_MODE_PWM1:
  457. case PM8XXX_LED_MODE_PWM2:
  458. case PM8XXX_LED_MODE_PWM3:
  459. led->reg = PM8XXX_FLASH_MODE_PWM;
  460. break;
  461. case PM8XXX_LED_MODE_DTEST1:
  462. led->reg = PM8XXX_FLASH_MODE_DBUS1;
  463. break;
  464. case PM8XXX_LED_MODE_DTEST2:
  465. led->reg = PM8XXX_FLASH_MODE_DBUS2;
  466. break;
  467. default:
  468. led->reg = PM8XXX_LED_MODE_MANUAL;
  469. break;
  470. }
  471. break;
  472. case PM8XXX_ID_WLED:
  473. led->cdev.max_brightness = WLED_MAX_LEVEL;
  474. break;
  475. case PM8XXX_ID_RGB_LED_RED:
  476. case PM8XXX_ID_RGB_LED_GREEN:
  477. case PM8XXX_ID_RGB_LED_BLUE:
  478. led->cdev.max_brightness = LED_FULL;
  479. break;
  480. default:
  481. dev_err(led->cdev.dev, "LED Id is invalid");
  482. return -EINVAL;
  483. }
  484. return 0;
  485. }
  486. static enum led_brightness pm8xxx_led_get(struct led_classdev *led_cdev)
  487. {
  488. struct pm8xxx_led_data *led;
  489. led = container_of(led_cdev, struct pm8xxx_led_data, cdev);
  490. return led->cdev.brightness;
  491. }
  492. static int __devinit init_wled(struct pm8xxx_led_data *led)
  493. {
  494. int rc, i;
  495. u8 val, string_max_current;
  496. /* program over voltage protection threshold */
  497. if (led->wled_cfg->ovp_val > WLED_OVP_37V) {
  498. dev_err(led->dev->parent, "Invalid ovp value");
  499. return -EINVAL;
  500. }
  501. rc = pm8xxx_readb(led->dev->parent, WLED_OVP_CFG_REG, &val);
  502. if (rc) {
  503. dev_err(led->dev->parent, "can't read wled ovp config"
  504. " register rc=%d\n", rc);
  505. return rc;
  506. }
  507. val = (val & ~WLED_OVP_VAL_MASK) |
  508. (led->wled_cfg->ovp_val << WLED_OVP_VAL_BIT_SHFT);
  509. rc = pm8xxx_writeb(led->dev->parent, WLED_OVP_CFG_REG, val);
  510. if (rc) {
  511. dev_err(led->dev->parent, "can't write wled ovp config"
  512. " register rc=%d\n", rc);
  513. return rc;
  514. }
  515. /* program current boost limit and output feedback*/
  516. if (led->wled_cfg->boost_curr_lim > WLED_CURR_LIMIT_1680mA) {
  517. dev_err(led->dev->parent, "Invalid boost current limit");
  518. return -EINVAL;
  519. }
  520. rc = pm8xxx_readb(led->dev->parent, WLED_BOOST_CFG_REG, &val);
  521. if (rc) {
  522. dev_err(led->dev->parent, "can't read wled boost config"
  523. " register rc=%d\n", rc);
  524. return rc;
  525. }
  526. val = (val & ~WLED_BOOST_LIMIT_MASK) |
  527. (led->wled_cfg->boost_curr_lim << WLED_BOOST_LIMIT_BIT_SHFT);
  528. val = (val & ~WLED_OP_FDBCK_MASK) |
  529. (led->wled_cfg->op_fdbck << WLED_OP_FDBCK_BIT_SHFT);
  530. rc = pm8xxx_writeb(led->dev->parent, WLED_BOOST_CFG_REG, val);
  531. if (rc) {
  532. dev_err(led->dev->parent, "can't write wled boost config"
  533. " register rc=%d\n", rc);
  534. return rc;
  535. }
  536. /* program high pole capacitance */
  537. if (led->wled_cfg->cp_select > WLED_CP_SELECT_MAX) {
  538. dev_err(led->dev->parent, "Invalid pole capacitance");
  539. return -EINVAL;
  540. }
  541. rc = pm8xxx_readb(led->dev->parent, WLED_HIGH_POLE_CAP_REG, &val);
  542. if (rc) {
  543. dev_err(led->dev->parent, "can't read wled high pole"
  544. " capacitance register rc=%d\n", rc);
  545. return rc;
  546. }
  547. val = (val & ~WLED_CP_SELECT_MASK) | led->wled_cfg->cp_select;
  548. rc = pm8xxx_writeb(led->dev->parent, WLED_HIGH_POLE_CAP_REG, val);
  549. if (rc) {
  550. dev_err(led->dev->parent, "can't write wled high pole"
  551. " capacitance register rc=%d\n", rc);
  552. return rc;
  553. }
  554. /* program activation delay and maximum current */
  555. for (i = 0; i < WLED_STRINGS; i++) {
  556. if (led->wled_cfg->strings && (1 << i)) {
  557. rc = pm8xxx_readb(led->dev->parent,
  558. WLED_MAX_CURR_CFG_REG(i), &val);
  559. if (rc) {
  560. dev_err(led->dev->parent,
  561. "can't read wled max current"
  562. " config register rc=%d\n", rc);
  563. return rc;
  564. }
  565. if ((led->wled_cfg->ctrl_delay_us % WLED_CTL_DLY_STEP)
  566. || (led->wled_cfg->ctrl_delay_us >
  567. WLED_CTL_DLY_MAX)) {
  568. dev_err(led->dev->parent,
  569. "Invalid control delay\n");
  570. return rc;
  571. }
  572. val = val / WLED_CTL_DLY_STEP;
  573. val = (val & ~WLED_CTL_DLY_MASK) |
  574. (led->wled_cfg->ctrl_delay_us <<
  575. WLED_CTL_DLY_BIT_SHFT);
  576. if ((led->max_current > WLED_MAX_CURR)) {
  577. dev_err(led->dev->parent,
  578. "Invalid max current\n");
  579. return -EINVAL;
  580. }
  581. if (led->wled_cfg->max_current_ind) {
  582. switch (i) {
  583. case WLED_STRING_ONE:
  584. string_max_current = led->wled_cfg->max_one;
  585. break;
  586. case WLED_STRING_TWO:
  587. string_max_current = led->wled_cfg->max_two;
  588. break;
  589. case WLED_STRING_THREE:
  590. string_max_current = led->wled_cfg->max_three;
  591. break;
  592. default:
  593. return -EINVAL;
  594. }
  595. val = (val & ~WLED_MAX_CURR_MASK) | string_max_current;
  596. } else
  597. val = (val & ~WLED_MAX_CURR_MASK) | led->max_current;
  598. rc = pm8xxx_writeb(led->dev->parent,
  599. WLED_MAX_CURR_CFG_REG(i), val);
  600. if (rc) {
  601. dev_err(led->dev->parent,
  602. "can't write wled max current"
  603. " config register rc=%d\n", rc);
  604. return rc;
  605. }
  606. }
  607. }
  608. if (led->wled_cfg->cabc_en) {
  609. rc = pm8xxx_readb(led->dev->parent, WLED_SYNC_REG, &val);
  610. if (rc) {
  611. dev_err(led->dev->parent,
  612. "can't read cabc register rc=%d\n", rc);
  613. return rc;
  614. }
  615. val |= (led->wled_cfg->strings << WLED_CABC_SHIFT);
  616. rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val);
  617. if (rc) {
  618. dev_err(led->dev->parent,
  619. "can't write to enable cabc rc=%d\n", rc);
  620. return rc;
  621. }
  622. }
  623. /* program digital module generator, cs out and enable the module */
  624. rc = pm8xxx_readb(led->dev->parent, WLED_MOD_CTRL_REG, &val);
  625. if (rc) {
  626. dev_err(led->dev->parent, "can't read wled module ctrl"
  627. " register rc=%d\n", rc);
  628. return rc;
  629. }
  630. if (led->wled_cfg->dig_mod_gen_en)
  631. val |= WLED_DIG_MOD_GEN_MASK;
  632. if (led->wled_cfg->cs_out_en)
  633. val |= WLED_CS_OUT_MASK;
  634. val |= WLED_EN_MASK;
  635. rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG, val);
  636. if (rc) {
  637. dev_err(led->dev->parent, "can't write wled module ctrl"
  638. " register rc=%d\n", rc);
  639. return rc;
  640. }
  641. led->wled_mod_ctrl_val = val;
  642. /* dump wled registers */
  643. wled_dump_regs(led);
  644. return 0;
  645. }
  646. static int __devinit get_init_value(struct pm8xxx_led_data *led, u8 *val)
  647. {
  648. int rc, offset;
  649. u16 addr;
  650. switch (led->id) {
  651. case PM8XXX_ID_LED_KB_LIGHT:
  652. addr = SSBI_REG_ADDR_DRV_KEYPAD;
  653. break;
  654. case PM8XXX_ID_LED_0:
  655. case PM8XXX_ID_LED_1:
  656. case PM8XXX_ID_LED_2:
  657. offset = PM8XXX_LED_OFFSET(led->id);
  658. addr = SSBI_REG_ADDR_LED_CTRL(offset);
  659. break;
  660. case PM8XXX_ID_FLASH_LED_0:
  661. addr = SSBI_REG_ADDR_FLASH_DRV0;
  662. break;
  663. case PM8XXX_ID_FLASH_LED_1:
  664. addr = SSBI_REG_ADDR_FLASH_DRV1;
  665. break;
  666. case PM8XXX_ID_WLED:
  667. rc = init_wled(led);
  668. if (rc)
  669. dev_err(led->cdev.dev, "can't initialize wled rc=%d\n",
  670. rc);
  671. return rc;
  672. case PM8XXX_ID_RGB_LED_RED:
  673. case PM8XXX_ID_RGB_LED_GREEN:
  674. case PM8XXX_ID_RGB_LED_BLUE:
  675. addr = SSBI_REG_ADDR_RGB_CNTL1;
  676. break;
  677. default:
  678. dev_err(led->cdev.dev, "unknown led id %d", led->id);
  679. return -EINVAL;
  680. }
  681. rc = pm8xxx_readb(led->dev->parent, addr, val);
  682. if (rc)
  683. dev_err(led->cdev.dev, "can't get led(%d) level rc=%d\n",
  684. led->id, rc);
  685. return rc;
  686. }
  687. static int pm8xxx_led_pwm_configure(struct pm8xxx_led_data *led)
  688. {
  689. int start_idx, idx_len, duty_us, rc, flags;
  690. led->pwm_dev = pwm_request(led->pwm_channel,
  691. led->cdev.name);
  692. if (IS_ERR_OR_NULL(led->pwm_dev)) {
  693. pr_err("could not acquire PWM Channel %d, "
  694. "error %ld\n", led->pwm_channel,
  695. PTR_ERR(led->pwm_dev));
  696. led->pwm_dev = NULL;
  697. return -ENODEV;
  698. }
  699. flags = PM8XXX_LED_PWM_FLAGS;
  700. switch (led->max_current) {
  701. case PM8XXX_PWM_CURRENT_4MA:
  702. flags |= PM_PWM_BANK_LO;
  703. break;
  704. case PM8XXX_PWM_CURRENT_8MA:
  705. flags |= PM_PWM_BANK_HI;
  706. break;
  707. case PM8XXX_PWM_CURRENT_12MA:
  708. flags |= (PM_PWM_BANK_LO | PM_PWM_BANK_HI);
  709. break;
  710. default:
  711. flags |= (PM_PWM_BANK_LO | PM_PWM_BANK_HI);
  712. break;
  713. }
  714. if (led->pwm_duty_cycles != NULL) {
  715. start_idx = led->pwm_duty_cycles->start_idx;
  716. idx_len = led->pwm_duty_cycles->num_duty_pcts;
  717. if (idx_len >= PM_PWM_LUT_SIZE && start_idx) {
  718. pr_err("Wrong LUT size or index\n");
  719. return -EINVAL;
  720. }
  721. if ((start_idx + idx_len) > PM_PWM_LUT_SIZE) {
  722. pr_err("Exceed LUT limit\n");
  723. return -EINVAL;
  724. }
  725. rc = pm8xxx_pwm_lut_config(led->pwm_dev, led->pwm_period_us,
  726. led->pwm_duty_cycles->duty_pcts,
  727. led->pwm_duty_cycles->duty_ms,
  728. start_idx, idx_len, 0, 0,
  729. flags);
  730. } else {
  731. duty_us = led->pwm_period_us;
  732. rc = pwm_config(led->pwm_dev, duty_us, led->pwm_period_us);
  733. }
  734. return rc;
  735. }
  736. static int __devinit pm8xxx_led_probe(struct platform_device *pdev)
  737. {
  738. const struct pm8xxx_led_platform_data *pdata = pdev->dev.platform_data;
  739. const struct led_platform_data *pcore_data;
  740. struct led_info *curr_led;
  741. struct pm8xxx_led_data *led, *led_dat;
  742. struct pm8xxx_led_config *led_cfg;
  743. enum pm8xxx_version version;
  744. bool found = false;
  745. int rc, i, j;
  746. if (pdata == NULL) {
  747. dev_err(&pdev->dev, "platform data not supplied\n");
  748. return -EINVAL;
  749. }
  750. pcore_data = pdata->led_core;
  751. if (pcore_data->num_leds != pdata->num_configs) {
  752. dev_err(&pdev->dev, "#no. of led configs and #no. of led"
  753. "entries are not equal\n");
  754. return -EINVAL;
  755. }
  756. led = kcalloc(pcore_data->num_leds, sizeof(*led), GFP_KERNEL);
  757. if (led == NULL) {
  758. dev_err(&pdev->dev, "failed to alloc memory\n");
  759. return -ENOMEM;
  760. }
  761. for (i = 0; i < pcore_data->num_leds; i++) {
  762. curr_led = &pcore_data->leds[i];
  763. led_dat = &led[i];
  764. led_cfg = &pdata->configs[i];
  765. led_dat->id = led_cfg->id;
  766. led_dat->pwm_channel = led_cfg->pwm_channel;
  767. led_dat->pwm_period_us = led_cfg->pwm_period_us;
  768. led_dat->pwm_duty_cycles = led_cfg->pwm_duty_cycles;
  769. led_dat->wled_cfg = led_cfg->wled_cfg;
  770. led_dat->max_current = led_cfg->max_current;
  771. if (!((led_dat->id >= PM8XXX_ID_LED_KB_LIGHT) &&
  772. (led_dat->id < PM8XXX_ID_MAX))) {
  773. dev_err(&pdev->dev, "invalid LED ID(%d) specified\n",
  774. led_dat->id);
  775. rc = -EINVAL;
  776. goto fail_id_check;
  777. }
  778. found = false;
  779. version = pm8xxx_get_version(pdev->dev.parent);
  780. for (j = 0; j < ARRAY_SIZE(led_map); j++) {
  781. if (version == led_map[j].version
  782. && (led_map[j].supported & (1 << led_dat->id))) {
  783. found = true;
  784. break;
  785. }
  786. }
  787. if (!found) {
  788. dev_err(&pdev->dev, "invalid LED ID(%d) specified\n",
  789. led_dat->id);
  790. rc = -EINVAL;
  791. goto fail_id_check;
  792. }
  793. led_dat->cdev.name = curr_led->name;
  794. led_dat->cdev.default_trigger = curr_led->default_trigger;
  795. led_dat->cdev.brightness_set = pm8xxx_led_set;
  796. led_dat->cdev.brightness_get = pm8xxx_led_get;
  797. led_dat->cdev.brightness = LED_OFF;
  798. led_dat->cdev.flags = curr_led->flags;
  799. led_dat->dev = &pdev->dev;
  800. rc = get_init_value(led_dat, &led_dat->reg);
  801. if (rc < 0)
  802. goto fail_id_check;
  803. rc = pm8xxx_set_led_mode_and_max_brightness(led_dat,
  804. led_cfg->mode, led_cfg->max_current);
  805. if (rc < 0)
  806. goto fail_id_check;
  807. mutex_init(&led_dat->lock);
  808. INIT_WORK(&led_dat->work, pm8xxx_led_work);
  809. rc = led_classdev_register(&pdev->dev, &led_dat->cdev);
  810. if (rc) {
  811. dev_err(&pdev->dev, "unable to register led %d,rc=%d\n",
  812. led_dat->id, rc);
  813. goto fail_id_check;
  814. }
  815. /* configure default state */
  816. if (led_cfg->default_state)
  817. led->cdev.brightness = led_dat->cdev.max_brightness;
  818. else
  819. led->cdev.brightness = LED_OFF;
  820. if (led_cfg->mode != PM8XXX_LED_MODE_MANUAL) {
  821. if (led_dat->id == PM8XXX_ID_RGB_LED_RED ||
  822. led_dat->id == PM8XXX_ID_RGB_LED_GREEN ||
  823. led_dat->id == PM8XXX_ID_RGB_LED_BLUE)
  824. __pm8xxx_led_work(led_dat, 0);
  825. else
  826. __pm8xxx_led_work(led_dat,
  827. led_dat->cdev.max_brightness);
  828. if (led_dat->pwm_channel != -1) {
  829. led_dat->cdev.max_brightness = LED_FULL;
  830. rc = pm8xxx_led_pwm_configure(led_dat);
  831. if (rc) {
  832. dev_err(&pdev->dev, "failed to "
  833. "configure LED, error: %d\n", rc);
  834. goto fail_id_check;
  835. }
  836. schedule_work(&led->work);
  837. }
  838. } else {
  839. __pm8xxx_led_work(led_dat, led->cdev.brightness);
  840. }
  841. }
  842. platform_set_drvdata(pdev, led);
  843. return 0;
  844. fail_id_check:
  845. if (i > 0) {
  846. for (i = i - 1; i >= 0; i--) {
  847. mutex_destroy(&led[i].lock);
  848. led_classdev_unregister(&led[i].cdev);
  849. if (led[i].pwm_dev != NULL)
  850. pwm_free(led[i].pwm_dev);
  851. }
  852. }
  853. kfree(led);
  854. return rc;
  855. }
  856. static int __devexit pm8xxx_led_remove(struct platform_device *pdev)
  857. {
  858. int i;
  859. const struct led_platform_data *pdata =
  860. pdev->dev.platform_data;
  861. struct pm8xxx_led_data *led = platform_get_drvdata(pdev);
  862. for (i = 0; i < pdata->num_leds; i++) {
  863. cancel_work_sync(&led[i].work);
  864. mutex_destroy(&led[i].lock);
  865. led_classdev_unregister(&led[i].cdev);
  866. if (led[i].pwm_dev != NULL)
  867. pwm_free(led[i].pwm_dev);
  868. }
  869. kfree(led);
  870. return 0;
  871. }
  872. static struct platform_driver pm8xxx_led_driver = {
  873. .probe = pm8xxx_led_probe,
  874. .remove = __devexit_p(pm8xxx_led_remove),
  875. .driver = {
  876. .name = PM8XXX_LEDS_DEV_NAME,
  877. .owner = THIS_MODULE,
  878. },
  879. };
  880. static int __init pm8xxx_led_init(void)
  881. {
  882. return platform_driver_register(&pm8xxx_led_driver);
  883. }
  884. subsys_initcall(pm8xxx_led_init);
  885. static void __exit pm8xxx_led_exit(void)
  886. {
  887. platform_driver_unregister(&pm8xxx_led_driver);
  888. }
  889. module_exit(pm8xxx_led_exit);
  890. MODULE_DESCRIPTION("PM8XXX LEDs driver");
  891. MODULE_LICENSE("GPL v2");
  892. MODULE_VERSION("1.0");
  893. MODULE_ALIAS("platform:pm8xxx-led");