88pm860x-core.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /*
  2. * Base driver for Marvell 88PM8607
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/i2c.h>
  15. #include <linux/irq.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/mfd/core.h>
  21. #include <linux/mfd/88pm860x.h>
  22. #include <linux/regulator/machine.h>
  23. #define INT_STATUS_NUM 3
  24. static struct resource bk0_resources[] __devinitdata = {
  25. {2, 2, "duty cycle", IORESOURCE_REG, },
  26. {3, 3, "always on", IORESOURCE_REG, },
  27. {3, 3, "current", IORESOURCE_REG, },
  28. };
  29. static struct resource bk1_resources[] __devinitdata = {
  30. {4, 4, "duty cycle", IORESOURCE_REG, },
  31. {5, 5, "always on", IORESOURCE_REG, },
  32. {5, 5, "current", IORESOURCE_REG, },
  33. };
  34. static struct resource bk2_resources[] __devinitdata = {
  35. {6, 6, "duty cycle", IORESOURCE_REG, },
  36. {7, 7, "always on", IORESOURCE_REG, },
  37. {5, 5, "current", IORESOURCE_REG, },
  38. };
  39. static struct resource led0_resources[] __devinitdata = {
  40. /* RGB1 Red LED */
  41. {0xd, 0xd, "control", IORESOURCE_REG, },
  42. {0xc, 0xc, "blink", IORESOURCE_REG, },
  43. };
  44. static struct resource led1_resources[] __devinitdata = {
  45. /* RGB1 Green LED */
  46. {0xe, 0xe, "control", IORESOURCE_REG, },
  47. {0xc, 0xc, "blink", IORESOURCE_REG, },
  48. };
  49. static struct resource led2_resources[] __devinitdata = {
  50. /* RGB1 Blue LED */
  51. {0xf, 0xf, "control", IORESOURCE_REG, },
  52. {0xc, 0xc, "blink", IORESOURCE_REG, },
  53. };
  54. static struct resource led3_resources[] __devinitdata = {
  55. /* RGB2 Red LED */
  56. {0x9, 0x9, "control", IORESOURCE_REG, },
  57. {0x8, 0x8, "blink", IORESOURCE_REG, },
  58. };
  59. static struct resource led4_resources[] __devinitdata = {
  60. /* RGB2 Green LED */
  61. {0xa, 0xa, "control", IORESOURCE_REG, },
  62. {0x8, 0x8, "blink", IORESOURCE_REG, },
  63. };
  64. static struct resource led5_resources[] __devinitdata = {
  65. /* RGB2 Blue LED */
  66. {0xb, 0xb, "control", IORESOURCE_REG, },
  67. {0x8, 0x8, "blink", IORESOURCE_REG, },
  68. };
  69. static struct resource buck1_resources[] __devinitdata = {
  70. {0x24, 0x24, "buck set", IORESOURCE_REG, },
  71. };
  72. static struct resource buck2_resources[] __devinitdata = {
  73. {0x25, 0x25, "buck set", IORESOURCE_REG, },
  74. };
  75. static struct resource buck3_resources[] __devinitdata = {
  76. {0x26, 0x26, "buck set", IORESOURCE_REG, },
  77. };
  78. static struct resource ldo1_resources[] __devinitdata = {
  79. {0x10, 0x10, "ldo set", IORESOURCE_REG, },
  80. };
  81. static struct resource ldo2_resources[] __devinitdata = {
  82. {0x11, 0x11, "ldo set", IORESOURCE_REG, },
  83. };
  84. static struct resource ldo3_resources[] __devinitdata = {
  85. {0x12, 0x12, "ldo set", IORESOURCE_REG, },
  86. };
  87. static struct resource ldo4_resources[] __devinitdata = {
  88. {0x13, 0x13, "ldo set", IORESOURCE_REG, },
  89. };
  90. static struct resource ldo5_resources[] __devinitdata = {
  91. {0x14, 0x14, "ldo set", IORESOURCE_REG, },
  92. };
  93. static struct resource ldo6_resources[] __devinitdata = {
  94. {0x15, 0x15, "ldo set", IORESOURCE_REG, },
  95. };
  96. static struct resource ldo7_resources[] __devinitdata = {
  97. {0x16, 0x16, "ldo set", IORESOURCE_REG, },
  98. };
  99. static struct resource ldo8_resources[] __devinitdata = {
  100. {0x17, 0x17, "ldo set", IORESOURCE_REG, },
  101. };
  102. static struct resource ldo9_resources[] __devinitdata = {
  103. {0x18, 0x18, "ldo set", IORESOURCE_REG, },
  104. };
  105. static struct resource ldo10_resources[] __devinitdata = {
  106. {0x19, 0x19, "ldo set", IORESOURCE_REG, },
  107. };
  108. static struct resource ldo12_resources[] __devinitdata = {
  109. {0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
  110. };
  111. static struct resource ldo_vibrator_resources[] __devinitdata = {
  112. {0x28, 0x28, "ldo set", IORESOURCE_REG, },
  113. };
  114. static struct resource ldo14_resources[] __devinitdata = {
  115. {0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
  116. };
  117. static struct resource touch_resources[] __devinitdata = {
  118. {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
  119. };
  120. static struct resource onkey_resources[] __devinitdata = {
  121. {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
  122. };
  123. static struct resource codec_resources[] __devinitdata = {
  124. /* Headset microphone insertion or removal */
  125. {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
  126. /* Hook-switch press or release */
  127. {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
  128. /* Headset insertion or removal */
  129. {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
  130. /* Audio short */
  131. {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
  132. };
  133. static struct resource battery_resources[] __devinitdata = {
  134. {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
  135. {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
  136. };
  137. static struct resource charger_resources[] __devinitdata = {
  138. {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
  139. {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,},
  140. {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout", IORESOURCE_IRQ,},
  141. {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,},
  142. {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
  143. {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
  144. };
  145. static struct resource rtc_resources[] __devinitdata = {
  146. {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
  147. };
  148. static struct mfd_cell bk_devs[] __devinitdata = {
  149. {
  150. .name = "88pm860x-backlight",
  151. .id = 0,
  152. .num_resources = ARRAY_SIZE(bk0_resources),
  153. .resources = bk0_resources,
  154. }, {
  155. .name = "88pm860x-backlight",
  156. .id = 1,
  157. .num_resources = ARRAY_SIZE(bk1_resources),
  158. .resources = bk1_resources,
  159. }, {
  160. .name = "88pm860x-backlight",
  161. .id = 2,
  162. .num_resources = ARRAY_SIZE(bk2_resources),
  163. .resources = bk2_resources,
  164. },
  165. };
  166. static struct mfd_cell led_devs[] __devinitdata = {
  167. {
  168. .name = "88pm860x-led",
  169. .id = 0,
  170. .num_resources = ARRAY_SIZE(led0_resources),
  171. .resources = led0_resources,
  172. }, {
  173. .name = "88pm860x-led",
  174. .id = 1,
  175. .num_resources = ARRAY_SIZE(led1_resources),
  176. .resources = led1_resources,
  177. }, {
  178. .name = "88pm860x-led",
  179. .id = 2,
  180. .num_resources = ARRAY_SIZE(led2_resources),
  181. .resources = led2_resources,
  182. }, {
  183. .name = "88pm860x-led",
  184. .id = 3,
  185. .num_resources = ARRAY_SIZE(led3_resources),
  186. .resources = led3_resources,
  187. }, {
  188. .name = "88pm860x-led",
  189. .id = 4,
  190. .num_resources = ARRAY_SIZE(led4_resources),
  191. .resources = led4_resources,
  192. }, {
  193. .name = "88pm860x-led",
  194. .id = 5,
  195. .num_resources = ARRAY_SIZE(led5_resources),
  196. .resources = led5_resources,
  197. },
  198. };
  199. static struct mfd_cell reg_devs[] __devinitdata = {
  200. {
  201. .name = "88pm860x-regulator",
  202. .id = 0,
  203. .num_resources = ARRAY_SIZE(buck1_resources),
  204. .resources = buck1_resources,
  205. }, {
  206. .name = "88pm860x-regulator",
  207. .id = 1,
  208. .num_resources = ARRAY_SIZE(buck2_resources),
  209. .resources = buck2_resources,
  210. }, {
  211. .name = "88pm860x-regulator",
  212. .id = 2,
  213. .num_resources = ARRAY_SIZE(buck3_resources),
  214. .resources = buck3_resources,
  215. }, {
  216. .name = "88pm860x-regulator",
  217. .id = 3,
  218. .num_resources = ARRAY_SIZE(ldo1_resources),
  219. .resources = ldo1_resources,
  220. }, {
  221. .name = "88pm860x-regulator",
  222. .id = 4,
  223. .num_resources = ARRAY_SIZE(ldo2_resources),
  224. .resources = ldo2_resources,
  225. }, {
  226. .name = "88pm860x-regulator",
  227. .id = 5,
  228. .num_resources = ARRAY_SIZE(ldo3_resources),
  229. .resources = ldo3_resources,
  230. }, {
  231. .name = "88pm860x-regulator",
  232. .id = 6,
  233. .num_resources = ARRAY_SIZE(ldo4_resources),
  234. .resources = ldo4_resources,
  235. }, {
  236. .name = "88pm860x-regulator",
  237. .id = 7,
  238. .num_resources = ARRAY_SIZE(ldo5_resources),
  239. .resources = ldo5_resources,
  240. }, {
  241. .name = "88pm860x-regulator",
  242. .id = 8,
  243. .num_resources = ARRAY_SIZE(ldo6_resources),
  244. .resources = ldo6_resources,
  245. }, {
  246. .name = "88pm860x-regulator",
  247. .id = 9,
  248. .num_resources = ARRAY_SIZE(ldo7_resources),
  249. .resources = ldo7_resources,
  250. }, {
  251. .name = "88pm860x-regulator",
  252. .id = 10,
  253. .num_resources = ARRAY_SIZE(ldo8_resources),
  254. .resources = ldo8_resources,
  255. }, {
  256. .name = "88pm860x-regulator",
  257. .id = 11,
  258. .num_resources = ARRAY_SIZE(ldo9_resources),
  259. .resources = ldo9_resources,
  260. }, {
  261. .name = "88pm860x-regulator",
  262. .id = 12,
  263. .num_resources = ARRAY_SIZE(ldo10_resources),
  264. .resources = ldo10_resources,
  265. }, {
  266. .name = "88pm860x-regulator",
  267. .id = 13,
  268. .num_resources = ARRAY_SIZE(ldo12_resources),
  269. .resources = ldo12_resources,
  270. }, {
  271. .name = "88pm860x-regulator",
  272. .id = 14,
  273. .num_resources = ARRAY_SIZE(ldo_vibrator_resources),
  274. .resources = ldo_vibrator_resources,
  275. }, {
  276. .name = "88pm860x-regulator",
  277. .id = 15,
  278. .num_resources = ARRAY_SIZE(ldo14_resources),
  279. .resources = ldo14_resources,
  280. },
  281. };
  282. static struct mfd_cell touch_devs[] = {
  283. {"88pm860x-touch", -1,},
  284. };
  285. static struct mfd_cell onkey_devs[] = {
  286. {"88pm860x-onkey", -1,},
  287. };
  288. static struct mfd_cell codec_devs[] = {
  289. {"88pm860x-codec", -1,},
  290. };
  291. static struct regulator_consumer_supply preg_supply[] = {
  292. REGULATOR_SUPPLY("preg", "charger-manager"),
  293. };
  294. static struct regulator_init_data preg_init_data = {
  295. .num_consumer_supplies = ARRAY_SIZE(preg_supply),
  296. .consumer_supplies = &preg_supply[0],
  297. };
  298. static struct mfd_cell power_devs[] = {
  299. {"88pm860x-battery", -1,},
  300. {"88pm860x-charger", -1,},
  301. {"88pm860x-preg", -1,},
  302. };
  303. static struct mfd_cell rtc_devs[] = {
  304. {"88pm860x-rtc", -1,},
  305. };
  306. struct pm860x_irq_data {
  307. int reg;
  308. int mask_reg;
  309. int enable; /* enable or not */
  310. int offs; /* bit offset in mask register */
  311. };
  312. static struct pm860x_irq_data pm860x_irqs[] = {
  313. [PM8607_IRQ_ONKEY] = {
  314. .reg = PM8607_INT_STATUS1,
  315. .mask_reg = PM8607_INT_MASK_1,
  316. .offs = 1 << 0,
  317. },
  318. [PM8607_IRQ_EXTON] = {
  319. .reg = PM8607_INT_STATUS1,
  320. .mask_reg = PM8607_INT_MASK_1,
  321. .offs = 1 << 1,
  322. },
  323. [PM8607_IRQ_CHG] = {
  324. .reg = PM8607_INT_STATUS1,
  325. .mask_reg = PM8607_INT_MASK_1,
  326. .offs = 1 << 2,
  327. },
  328. [PM8607_IRQ_BAT] = {
  329. .reg = PM8607_INT_STATUS1,
  330. .mask_reg = PM8607_INT_MASK_1,
  331. .offs = 1 << 3,
  332. },
  333. [PM8607_IRQ_RTC] = {
  334. .reg = PM8607_INT_STATUS1,
  335. .mask_reg = PM8607_INT_MASK_1,
  336. .offs = 1 << 4,
  337. },
  338. [PM8607_IRQ_CC] = {
  339. .reg = PM8607_INT_STATUS1,
  340. .mask_reg = PM8607_INT_MASK_1,
  341. .offs = 1 << 5,
  342. },
  343. [PM8607_IRQ_VBAT] = {
  344. .reg = PM8607_INT_STATUS2,
  345. .mask_reg = PM8607_INT_MASK_2,
  346. .offs = 1 << 0,
  347. },
  348. [PM8607_IRQ_VCHG] = {
  349. .reg = PM8607_INT_STATUS2,
  350. .mask_reg = PM8607_INT_MASK_2,
  351. .offs = 1 << 1,
  352. },
  353. [PM8607_IRQ_VSYS] = {
  354. .reg = PM8607_INT_STATUS2,
  355. .mask_reg = PM8607_INT_MASK_2,
  356. .offs = 1 << 2,
  357. },
  358. [PM8607_IRQ_TINT] = {
  359. .reg = PM8607_INT_STATUS2,
  360. .mask_reg = PM8607_INT_MASK_2,
  361. .offs = 1 << 3,
  362. },
  363. [PM8607_IRQ_GPADC0] = {
  364. .reg = PM8607_INT_STATUS2,
  365. .mask_reg = PM8607_INT_MASK_2,
  366. .offs = 1 << 4,
  367. },
  368. [PM8607_IRQ_GPADC1] = {
  369. .reg = PM8607_INT_STATUS2,
  370. .mask_reg = PM8607_INT_MASK_2,
  371. .offs = 1 << 5,
  372. },
  373. [PM8607_IRQ_GPADC2] = {
  374. .reg = PM8607_INT_STATUS2,
  375. .mask_reg = PM8607_INT_MASK_2,
  376. .offs = 1 << 6,
  377. },
  378. [PM8607_IRQ_GPADC3] = {
  379. .reg = PM8607_INT_STATUS2,
  380. .mask_reg = PM8607_INT_MASK_2,
  381. .offs = 1 << 7,
  382. },
  383. [PM8607_IRQ_AUDIO_SHORT] = {
  384. .reg = PM8607_INT_STATUS3,
  385. .mask_reg = PM8607_INT_MASK_3,
  386. .offs = 1 << 0,
  387. },
  388. [PM8607_IRQ_PEN] = {
  389. .reg = PM8607_INT_STATUS3,
  390. .mask_reg = PM8607_INT_MASK_3,
  391. .offs = 1 << 1,
  392. },
  393. [PM8607_IRQ_HEADSET] = {
  394. .reg = PM8607_INT_STATUS3,
  395. .mask_reg = PM8607_INT_MASK_3,
  396. .offs = 1 << 2,
  397. },
  398. [PM8607_IRQ_HOOK] = {
  399. .reg = PM8607_INT_STATUS3,
  400. .mask_reg = PM8607_INT_MASK_3,
  401. .offs = 1 << 3,
  402. },
  403. [PM8607_IRQ_MICIN] = {
  404. .reg = PM8607_INT_STATUS3,
  405. .mask_reg = PM8607_INT_MASK_3,
  406. .offs = 1 << 4,
  407. },
  408. [PM8607_IRQ_CHG_FAIL] = {
  409. .reg = PM8607_INT_STATUS3,
  410. .mask_reg = PM8607_INT_MASK_3,
  411. .offs = 1 << 5,
  412. },
  413. [PM8607_IRQ_CHG_DONE] = {
  414. .reg = PM8607_INT_STATUS3,
  415. .mask_reg = PM8607_INT_MASK_3,
  416. .offs = 1 << 6,
  417. },
  418. [PM8607_IRQ_CHG_FAULT] = {
  419. .reg = PM8607_INT_STATUS3,
  420. .mask_reg = PM8607_INT_MASK_3,
  421. .offs = 1 << 7,
  422. },
  423. };
  424. static irqreturn_t pm860x_irq(int irq, void *data)
  425. {
  426. struct pm860x_chip *chip = data;
  427. struct pm860x_irq_data *irq_data;
  428. struct i2c_client *i2c;
  429. int read_reg = -1, value = 0;
  430. int i;
  431. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  432. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  433. irq_data = &pm860x_irqs[i];
  434. if (read_reg != irq_data->reg) {
  435. read_reg = irq_data->reg;
  436. value = pm860x_reg_read(i2c, irq_data->reg);
  437. }
  438. if (value & irq_data->enable)
  439. handle_nested_irq(chip->irq_base + i);
  440. }
  441. return IRQ_HANDLED;
  442. }
  443. static void pm860x_irq_lock(struct irq_data *data)
  444. {
  445. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  446. mutex_lock(&chip->irq_lock);
  447. }
  448. static void pm860x_irq_sync_unlock(struct irq_data *data)
  449. {
  450. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  451. struct pm860x_irq_data *irq_data;
  452. struct i2c_client *i2c;
  453. static unsigned char cached[3] = {0x0, 0x0, 0x0};
  454. unsigned char mask[3];
  455. int i;
  456. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  457. /* Load cached value. In initial, all IRQs are masked */
  458. for (i = 0; i < 3; i++)
  459. mask[i] = cached[i];
  460. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  461. irq_data = &pm860x_irqs[i];
  462. switch (irq_data->mask_reg) {
  463. case PM8607_INT_MASK_1:
  464. mask[0] &= ~irq_data->offs;
  465. mask[0] |= irq_data->enable;
  466. break;
  467. case PM8607_INT_MASK_2:
  468. mask[1] &= ~irq_data->offs;
  469. mask[1] |= irq_data->enable;
  470. break;
  471. case PM8607_INT_MASK_3:
  472. mask[2] &= ~irq_data->offs;
  473. mask[2] |= irq_data->enable;
  474. break;
  475. default:
  476. dev_err(chip->dev, "wrong IRQ\n");
  477. break;
  478. }
  479. }
  480. /* update mask into registers */
  481. for (i = 0; i < 3; i++) {
  482. if (mask[i] != cached[i]) {
  483. cached[i] = mask[i];
  484. pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
  485. }
  486. }
  487. mutex_unlock(&chip->irq_lock);
  488. }
  489. static void pm860x_irq_enable(struct irq_data *data)
  490. {
  491. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  492. pm860x_irqs[data->irq - chip->irq_base].enable
  493. = pm860x_irqs[data->irq - chip->irq_base].offs;
  494. }
  495. static void pm860x_irq_disable(struct irq_data *data)
  496. {
  497. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  498. pm860x_irqs[data->irq - chip->irq_base].enable = 0;
  499. }
  500. static struct irq_chip pm860x_irq_chip = {
  501. .name = "88pm860x",
  502. .irq_bus_lock = pm860x_irq_lock,
  503. .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
  504. .irq_enable = pm860x_irq_enable,
  505. .irq_disable = pm860x_irq_disable,
  506. };
  507. static int __devinit device_gpadc_init(struct pm860x_chip *chip,
  508. struct pm860x_platform_data *pdata)
  509. {
  510. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  511. : chip->companion;
  512. int data;
  513. int ret;
  514. /* initialize GPADC without activating it */
  515. if (!pdata || !pdata->touch)
  516. return -EINVAL;
  517. /* set GPADC MISC1 register */
  518. data = 0;
  519. data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
  520. data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  521. data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  522. data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
  523. if (data) {
  524. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  525. if (ret < 0)
  526. goto out;
  527. }
  528. /* set tsi prebias time */
  529. if (pdata->touch->tsi_prebias) {
  530. data = pdata->touch->tsi_prebias;
  531. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  532. if (ret < 0)
  533. goto out;
  534. }
  535. /* set prebias & prechg time of pen detect */
  536. data = 0;
  537. data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
  538. data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
  539. if (data) {
  540. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  541. if (ret < 0)
  542. goto out;
  543. }
  544. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
  545. PM8607_GPADC_EN, PM8607_GPADC_EN);
  546. out:
  547. return ret;
  548. }
  549. static int __devinit device_irq_init(struct pm860x_chip *chip,
  550. struct pm860x_platform_data *pdata)
  551. {
  552. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  553. : chip->companion;
  554. unsigned char status_buf[INT_STATUS_NUM];
  555. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  556. int i, data, mask, ret = -EINVAL;
  557. int __irq;
  558. if (!pdata || !pdata->irq_base) {
  559. dev_warn(chip->dev, "No interrupt support on IRQ base\n");
  560. return -EINVAL;
  561. }
  562. mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
  563. | PM8607_B0_MISC1_INT_MASK;
  564. data = 0;
  565. chip->irq_mode = 0;
  566. if (pdata && pdata->irq_mode) {
  567. /*
  568. * irq_mode defines the way of clearing interrupt. If it's 1,
  569. * clear IRQ by write. Otherwise, clear it by read.
  570. * This control bit is valid from 88PM8607 B0 steping.
  571. */
  572. data |= PM8607_B0_MISC1_INT_CLEAR;
  573. chip->irq_mode = 1;
  574. }
  575. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
  576. if (ret < 0)
  577. goto out;
  578. /* mask all IRQs */
  579. memset(status_buf, 0, INT_STATUS_NUM);
  580. ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
  581. INT_STATUS_NUM, status_buf);
  582. if (ret < 0)
  583. goto out;
  584. if (chip->irq_mode) {
  585. /* clear interrupt status by write */
  586. memset(status_buf, 0xFF, INT_STATUS_NUM);
  587. ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
  588. INT_STATUS_NUM, status_buf);
  589. } else {
  590. /* clear interrupt status by read */
  591. ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
  592. INT_STATUS_NUM, status_buf);
  593. }
  594. if (ret < 0)
  595. goto out;
  596. mutex_init(&chip->irq_lock);
  597. chip->irq_base = pdata->irq_base;
  598. chip->core_irq = i2c->irq;
  599. if (!chip->core_irq)
  600. goto out;
  601. /* register IRQ by genirq */
  602. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  603. __irq = i + chip->irq_base;
  604. irq_set_chip_data(__irq, chip);
  605. irq_set_chip_and_handler(__irq, &pm860x_irq_chip,
  606. handle_edge_irq);
  607. irq_set_nested_thread(__irq, 1);
  608. #ifdef CONFIG_ARM
  609. set_irq_flags(__irq, IRQF_VALID);
  610. #else
  611. irq_set_noprobe(__irq);
  612. #endif
  613. }
  614. ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags | IRQF_ONESHOT,
  615. "88pm860x", chip);
  616. if (ret) {
  617. dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
  618. chip->core_irq = 0;
  619. }
  620. return 0;
  621. out:
  622. chip->core_irq = 0;
  623. return ret;
  624. }
  625. static void device_irq_exit(struct pm860x_chip *chip)
  626. {
  627. if (chip->core_irq)
  628. free_irq(chip->core_irq, chip);
  629. }
  630. int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
  631. {
  632. int ret = -EIO;
  633. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  634. chip->client : chip->companion;
  635. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  636. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  637. __func__, chip->osc_vote,
  638. chip->osc_status);
  639. mutex_lock(&chip->osc_lock);
  640. /* Update voting status */
  641. chip->osc_vote |= client;
  642. /* If reference group is off - turn on*/
  643. if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
  644. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  645. /* Enable Reference group Vsys */
  646. if (pm860x_set_bits(i2c, PM8606_VSYS,
  647. PM8606_VSYS_EN, PM8606_VSYS_EN))
  648. goto out;
  649. /*Enable Internal Oscillator */
  650. if (pm860x_set_bits(i2c, PM8606_MISC,
  651. PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
  652. goto out;
  653. /* Update status (only if writes succeed) */
  654. chip->osc_status = PM8606_REF_GP_OSC_ON;
  655. }
  656. mutex_unlock(&chip->osc_lock);
  657. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  658. __func__, chip->osc_vote,
  659. chip->osc_status, ret);
  660. return 0;
  661. out:
  662. mutex_unlock(&chip->osc_lock);
  663. return ret;
  664. }
  665. EXPORT_SYMBOL(pm8606_osc_enable);
  666. int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
  667. {
  668. int ret = -EIO;
  669. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  670. chip->client : chip->companion;
  671. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  672. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  673. __func__, chip->osc_vote,
  674. chip->osc_status);
  675. mutex_lock(&chip->osc_lock);
  676. /*Update voting status */
  677. chip->osc_vote &= ~(client);
  678. /* If reference group is off and this is the last client to release
  679. * - turn off */
  680. if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
  681. (chip->osc_vote == REF_GP_NO_CLIENTS)) {
  682. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  683. /* Disable Reference group Vsys */
  684. if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
  685. goto out;
  686. /* Disable Internal Oscillator */
  687. if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
  688. goto out;
  689. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  690. }
  691. mutex_unlock(&chip->osc_lock);
  692. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  693. __func__, chip->osc_vote,
  694. chip->osc_status, ret);
  695. return 0;
  696. out:
  697. mutex_unlock(&chip->osc_lock);
  698. return ret;
  699. }
  700. EXPORT_SYMBOL(pm8606_osc_disable);
  701. static void __devinit device_osc_init(struct i2c_client *i2c)
  702. {
  703. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  704. mutex_init(&chip->osc_lock);
  705. /* init portofino reference group voting and status */
  706. /* Disable Reference group Vsys */
  707. pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
  708. /* Disable Internal Oscillator */
  709. pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
  710. chip->osc_vote = REF_GP_NO_CLIENTS;
  711. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  712. }
  713. static void __devinit device_bk_init(struct pm860x_chip *chip,
  714. struct pm860x_platform_data *pdata)
  715. {
  716. int ret, i;
  717. if (pdata && pdata->backlight) {
  718. if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
  719. pdata->num_backlights = ARRAY_SIZE(bk_devs);
  720. for (i = 0; i < pdata->num_backlights; i++) {
  721. bk_devs[i].platform_data = &pdata->backlight[i];
  722. bk_devs[i].pdata_size =
  723. sizeof(struct pm860x_backlight_pdata);
  724. }
  725. }
  726. ret = mfd_add_devices(chip->dev, 0, bk_devs,
  727. ARRAY_SIZE(bk_devs), NULL, 0, NULL);
  728. if (ret < 0)
  729. dev_err(chip->dev, "Failed to add backlight subdev\n");
  730. }
  731. static void __devinit device_led_init(struct pm860x_chip *chip,
  732. struct pm860x_platform_data *pdata)
  733. {
  734. int ret, i;
  735. if (pdata && pdata->led) {
  736. if (pdata->num_leds > ARRAY_SIZE(led_devs))
  737. pdata->num_leds = ARRAY_SIZE(led_devs);
  738. for (i = 0; i < pdata->num_leds; i++) {
  739. led_devs[i].platform_data = &pdata->led[i];
  740. led_devs[i].pdata_size =
  741. sizeof(struct pm860x_led_pdata);
  742. }
  743. }
  744. ret = mfd_add_devices(chip->dev, 0, led_devs,
  745. ARRAY_SIZE(led_devs), NULL, 0, NULL);
  746. if (ret < 0) {
  747. dev_err(chip->dev, "Failed to add led subdev\n");
  748. return;
  749. }
  750. }
  751. static void __devinit device_regulator_init(struct pm860x_chip *chip,
  752. struct pm860x_platform_data *pdata)
  753. {
  754. int ret;
  755. if (pdata == NULL)
  756. return;
  757. if (pdata->buck1) {
  758. reg_devs[0].platform_data = pdata->buck1;
  759. reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
  760. }
  761. if (pdata->buck2) {
  762. reg_devs[1].platform_data = pdata->buck2;
  763. reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
  764. }
  765. if (pdata->buck3) {
  766. reg_devs[2].platform_data = pdata->buck3;
  767. reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
  768. }
  769. if (pdata->ldo1) {
  770. reg_devs[3].platform_data = pdata->ldo1;
  771. reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
  772. }
  773. if (pdata->ldo2) {
  774. reg_devs[4].platform_data = pdata->ldo2;
  775. reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
  776. }
  777. if (pdata->ldo3) {
  778. reg_devs[5].platform_data = pdata->ldo3;
  779. reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
  780. }
  781. if (pdata->ldo4) {
  782. reg_devs[6].platform_data = pdata->ldo4;
  783. reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
  784. }
  785. if (pdata->ldo5) {
  786. reg_devs[7].platform_data = pdata->ldo5;
  787. reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
  788. }
  789. if (pdata->ldo6) {
  790. reg_devs[8].platform_data = pdata->ldo6;
  791. reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
  792. }
  793. if (pdata->ldo7) {
  794. reg_devs[9].platform_data = pdata->ldo7;
  795. reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
  796. }
  797. if (pdata->ldo8) {
  798. reg_devs[10].platform_data = pdata->ldo8;
  799. reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
  800. }
  801. if (pdata->ldo9) {
  802. reg_devs[11].platform_data = pdata->ldo9;
  803. reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
  804. }
  805. if (pdata->ldo10) {
  806. reg_devs[12].platform_data = pdata->ldo10;
  807. reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
  808. }
  809. if (pdata->ldo12) {
  810. reg_devs[13].platform_data = pdata->ldo12;
  811. reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
  812. }
  813. if (pdata->ldo_vibrator) {
  814. reg_devs[14].platform_data = pdata->ldo_vibrator;
  815. reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
  816. }
  817. if (pdata->ldo14) {
  818. reg_devs[15].platform_data = pdata->ldo14;
  819. reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
  820. }
  821. ret = mfd_add_devices(chip->dev, 0, reg_devs,
  822. ARRAY_SIZE(reg_devs), NULL, 0, NULL);
  823. if (ret < 0) {
  824. dev_err(chip->dev, "Failed to add regulator subdev\n");
  825. return;
  826. }
  827. }
  828. static void __devinit device_rtc_init(struct pm860x_chip *chip,
  829. struct pm860x_platform_data *pdata)
  830. {
  831. int ret;
  832. if ((pdata == NULL))
  833. return;
  834. rtc_devs[0].platform_data = pdata->rtc;
  835. rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
  836. rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
  837. rtc_devs[0].resources = &rtc_resources[0];
  838. ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
  839. ARRAY_SIZE(rtc_devs), &rtc_resources[0],
  840. chip->irq_base, NULL);
  841. if (ret < 0)
  842. dev_err(chip->dev, "Failed to add rtc subdev\n");
  843. }
  844. static void __devinit device_touch_init(struct pm860x_chip *chip,
  845. struct pm860x_platform_data *pdata)
  846. {
  847. int ret;
  848. if (pdata == NULL)
  849. return;
  850. touch_devs[0].platform_data = pdata->touch;
  851. touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
  852. touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
  853. touch_devs[0].resources = &touch_resources[0];
  854. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  855. ARRAY_SIZE(touch_devs), &touch_resources[0],
  856. chip->irq_base, NULL);
  857. if (ret < 0)
  858. dev_err(chip->dev, "Failed to add touch subdev\n");
  859. }
  860. static void __devinit device_power_init(struct pm860x_chip *chip,
  861. struct pm860x_platform_data *pdata)
  862. {
  863. int ret;
  864. if (pdata == NULL)
  865. return;
  866. power_devs[0].platform_data = pdata->power;
  867. power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
  868. power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
  869. power_devs[0].resources = &battery_resources[0],
  870. ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
  871. &battery_resources[0], chip->irq_base, NULL);
  872. if (ret < 0)
  873. dev_err(chip->dev, "Failed to add battery subdev\n");
  874. power_devs[1].platform_data = pdata->power;
  875. power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
  876. power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
  877. power_devs[1].resources = &charger_resources[0],
  878. ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
  879. &charger_resources[0], chip->irq_base, NULL);
  880. if (ret < 0)
  881. dev_err(chip->dev, "Failed to add charger subdev\n");
  882. power_devs[2].platform_data = &preg_init_data;
  883. power_devs[2].pdata_size = sizeof(struct regulator_init_data);
  884. ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
  885. NULL, chip->irq_base, NULL);
  886. if (ret < 0)
  887. dev_err(chip->dev, "Failed to add preg subdev\n");
  888. }
  889. static void __devinit device_onkey_init(struct pm860x_chip *chip,
  890. struct pm860x_platform_data *pdata)
  891. {
  892. int ret;
  893. onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
  894. onkey_devs[0].resources = &onkey_resources[0],
  895. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  896. ARRAY_SIZE(onkey_devs), &onkey_resources[0],
  897. chip->irq_base, NULL);
  898. if (ret < 0)
  899. dev_err(chip->dev, "Failed to add onkey subdev\n");
  900. }
  901. static void __devinit device_codec_init(struct pm860x_chip *chip,
  902. struct pm860x_platform_data *pdata)
  903. {
  904. int ret;
  905. codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
  906. codec_devs[0].resources = &codec_resources[0],
  907. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  908. ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
  909. NULL);
  910. if (ret < 0)
  911. dev_err(chip->dev, "Failed to add codec subdev\n");
  912. }
  913. static void __devinit device_8607_init(struct pm860x_chip *chip,
  914. struct i2c_client *i2c,
  915. struct pm860x_platform_data *pdata)
  916. {
  917. int data, ret;
  918. ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
  919. if (ret < 0) {
  920. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  921. goto out;
  922. }
  923. switch (ret & PM8607_VERSION_MASK) {
  924. case 0x40:
  925. case 0x50:
  926. dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
  927. ret);
  928. break;
  929. default:
  930. dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
  931. "Chip ID: %02x\n", ret);
  932. goto out;
  933. }
  934. ret = pm860x_reg_read(i2c, PM8607_BUCK3);
  935. if (ret < 0) {
  936. dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
  937. goto out;
  938. }
  939. if (ret & PM8607_BUCK3_DOUBLE)
  940. chip->buck3_double = 1;
  941. ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
  942. if (ret < 0) {
  943. dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
  944. goto out;
  945. }
  946. if (pdata && (pdata->i2c_port == PI2C_PORT))
  947. data = PM8607_B0_MISC1_PI2C;
  948. else
  949. data = 0;
  950. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
  951. if (ret < 0) {
  952. dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
  953. goto out;
  954. }
  955. ret = device_gpadc_init(chip, pdata);
  956. if (ret < 0)
  957. goto out;
  958. ret = device_irq_init(chip, pdata);
  959. if (ret < 0)
  960. goto out;
  961. device_regulator_init(chip, pdata);
  962. device_rtc_init(chip, pdata);
  963. device_onkey_init(chip, pdata);
  964. device_touch_init(chip, pdata);
  965. device_power_init(chip, pdata);
  966. device_codec_init(chip, pdata);
  967. out:
  968. return;
  969. }
  970. static void __devinit device_8606_init(struct pm860x_chip *chip,
  971. struct i2c_client *i2c,
  972. struct pm860x_platform_data *pdata)
  973. {
  974. device_osc_init(i2c);
  975. device_bk_init(chip, pdata);
  976. device_led_init(chip, pdata);
  977. }
  978. static int __devinit pm860x_device_init(struct pm860x_chip *chip,
  979. struct pm860x_platform_data *pdata)
  980. {
  981. chip->core_irq = 0;
  982. switch (chip->id) {
  983. case CHIP_PM8606:
  984. device_8606_init(chip, chip->client, pdata);
  985. break;
  986. case CHIP_PM8607:
  987. device_8607_init(chip, chip->client, pdata);
  988. break;
  989. }
  990. if (chip->companion) {
  991. switch (chip->id) {
  992. case CHIP_PM8607:
  993. device_8606_init(chip, chip->companion, pdata);
  994. break;
  995. case CHIP_PM8606:
  996. device_8607_init(chip, chip->companion, pdata);
  997. break;
  998. }
  999. }
  1000. return 0;
  1001. }
  1002. static void __devexit pm860x_device_exit(struct pm860x_chip *chip)
  1003. {
  1004. device_irq_exit(chip);
  1005. mfd_remove_devices(chip->dev);
  1006. }
  1007. static const struct i2c_device_id pm860x_id_table[] = {
  1008. { "88PM860x", 0 },
  1009. {}
  1010. };
  1011. MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
  1012. static int verify_addr(struct i2c_client *i2c)
  1013. {
  1014. unsigned short addr_8607[] = {0x30, 0x34};
  1015. unsigned short addr_8606[] = {0x10, 0x11};
  1016. int size, i;
  1017. if (i2c == NULL)
  1018. return 0;
  1019. size = ARRAY_SIZE(addr_8606);
  1020. for (i = 0; i < size; i++) {
  1021. if (i2c->addr == *(addr_8606 + i))
  1022. return CHIP_PM8606;
  1023. }
  1024. size = ARRAY_SIZE(addr_8607);
  1025. for (i = 0; i < size; i++) {
  1026. if (i2c->addr == *(addr_8607 + i))
  1027. return CHIP_PM8607;
  1028. }
  1029. return 0;
  1030. }
  1031. static struct regmap_config pm860x_regmap_config = {
  1032. .reg_bits = 8,
  1033. .val_bits = 8,
  1034. };
  1035. static int __devinit pm860x_probe(struct i2c_client *client,
  1036. const struct i2c_device_id *id)
  1037. {
  1038. struct pm860x_platform_data *pdata = client->dev.platform_data;
  1039. struct pm860x_chip *chip;
  1040. int ret;
  1041. if (!pdata) {
  1042. pr_info("No platform data in %s!\n", __func__);
  1043. return -EINVAL;
  1044. }
  1045. chip = kzalloc(sizeof(struct pm860x_chip), GFP_KERNEL);
  1046. if (chip == NULL)
  1047. return -ENOMEM;
  1048. chip->id = verify_addr(client);
  1049. chip->regmap = regmap_init_i2c(client, &pm860x_regmap_config);
  1050. if (IS_ERR(chip->regmap)) {
  1051. ret = PTR_ERR(chip->regmap);
  1052. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  1053. ret);
  1054. kfree(chip);
  1055. return ret;
  1056. }
  1057. chip->client = client;
  1058. i2c_set_clientdata(client, chip);
  1059. chip->dev = &client->dev;
  1060. dev_set_drvdata(chip->dev, chip);
  1061. /*
  1062. * Both client and companion client shares same platform driver.
  1063. * Driver distinguishes them by pdata->companion_addr.
  1064. * pdata->companion_addr is only assigned if companion chip exists.
  1065. * At the same time, the companion_addr shouldn't equal to client
  1066. * address.
  1067. */
  1068. if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
  1069. chip->companion_addr = pdata->companion_addr;
  1070. chip->companion = i2c_new_dummy(chip->client->adapter,
  1071. chip->companion_addr);
  1072. chip->regmap_companion = regmap_init_i2c(chip->companion,
  1073. &pm860x_regmap_config);
  1074. if (IS_ERR(chip->regmap_companion)) {
  1075. ret = PTR_ERR(chip->regmap_companion);
  1076. dev_err(&chip->companion->dev,
  1077. "Failed to allocate register map: %d\n", ret);
  1078. return ret;
  1079. }
  1080. i2c_set_clientdata(chip->companion, chip);
  1081. }
  1082. pm860x_device_init(chip, pdata);
  1083. return 0;
  1084. }
  1085. static int __devexit pm860x_remove(struct i2c_client *client)
  1086. {
  1087. struct pm860x_chip *chip = i2c_get_clientdata(client);
  1088. pm860x_device_exit(chip);
  1089. if (chip->companion) {
  1090. regmap_exit(chip->regmap_companion);
  1091. i2c_unregister_device(chip->companion);
  1092. }
  1093. regmap_exit(chip->regmap);
  1094. kfree(chip);
  1095. return 0;
  1096. }
  1097. #ifdef CONFIG_PM_SLEEP
  1098. static int pm860x_suspend(struct device *dev)
  1099. {
  1100. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1101. struct pm860x_chip *chip = i2c_get_clientdata(client);
  1102. if (device_may_wakeup(dev) && chip->wakeup_flag)
  1103. enable_irq_wake(chip->core_irq);
  1104. return 0;
  1105. }
  1106. static int pm860x_resume(struct device *dev)
  1107. {
  1108. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1109. struct pm860x_chip *chip = i2c_get_clientdata(client);
  1110. if (device_may_wakeup(dev) && chip->wakeup_flag)
  1111. disable_irq_wake(chip->core_irq);
  1112. return 0;
  1113. }
  1114. #endif
  1115. static SIMPLE_DEV_PM_OPS(pm860x_pm_ops, pm860x_suspend, pm860x_resume);
  1116. static struct i2c_driver pm860x_driver = {
  1117. .driver = {
  1118. .name = "88PM860x",
  1119. .owner = THIS_MODULE,
  1120. .pm = &pm860x_pm_ops,
  1121. },
  1122. .probe = pm860x_probe,
  1123. .remove = __devexit_p(pm860x_remove),
  1124. .id_table = pm860x_id_table,
  1125. };
  1126. static int __init pm860x_i2c_init(void)
  1127. {
  1128. int ret;
  1129. ret = i2c_add_driver(&pm860x_driver);
  1130. if (ret != 0)
  1131. pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
  1132. return ret;
  1133. }
  1134. subsys_initcall(pm860x_i2c_init);
  1135. static void __exit pm860x_i2c_exit(void)
  1136. {
  1137. i2c_del_driver(&pm860x_driver);
  1138. }
  1139. module_exit(pm860x_i2c_exit);
  1140. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
  1141. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  1142. MODULE_LICENSE("GPL");