88pm860x-core.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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/i2c.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/mfd/88pm860x.h>
  19. #include <linux/regulator/machine.h>
  20. #define INT_STATUS_NUM 3
  21. static struct resource bk_resources[] __devinitdata = {
  22. {PM8606_BACKLIGHT1, PM8606_BACKLIGHT1, "backlight-0", IORESOURCE_IO,},
  23. {PM8606_BACKLIGHT2, PM8606_BACKLIGHT2, "backlight-1", IORESOURCE_IO,},
  24. {PM8606_BACKLIGHT3, PM8606_BACKLIGHT3, "backlight-2", IORESOURCE_IO,},
  25. };
  26. static struct resource led_resources[] __devinitdata = {
  27. {PM8606_LED1_RED, PM8606_LED1_RED, "led0-red", IORESOURCE_IO,},
  28. {PM8606_LED1_GREEN, PM8606_LED1_GREEN, "led0-green", IORESOURCE_IO,},
  29. {PM8606_LED1_BLUE, PM8606_LED1_BLUE, "led0-blue", IORESOURCE_IO,},
  30. {PM8606_LED2_RED, PM8606_LED2_RED, "led1-red", IORESOURCE_IO,},
  31. {PM8606_LED2_GREEN, PM8606_LED2_GREEN, "led1-green", IORESOURCE_IO,},
  32. {PM8606_LED2_BLUE, PM8606_LED2_BLUE, "led1-blue", IORESOURCE_IO,},
  33. };
  34. static struct resource regulator_resources[] __devinitdata = {
  35. {PM8607_ID_BUCK1, PM8607_ID_BUCK1, "buck-1", IORESOURCE_IO,},
  36. {PM8607_ID_BUCK2, PM8607_ID_BUCK2, "buck-2", IORESOURCE_IO,},
  37. {PM8607_ID_BUCK3, PM8607_ID_BUCK3, "buck-3", IORESOURCE_IO,},
  38. {PM8607_ID_LDO1, PM8607_ID_LDO1, "ldo-01", IORESOURCE_IO,},
  39. {PM8607_ID_LDO2, PM8607_ID_LDO2, "ldo-02", IORESOURCE_IO,},
  40. {PM8607_ID_LDO3, PM8607_ID_LDO3, "ldo-03", IORESOURCE_IO,},
  41. {PM8607_ID_LDO4, PM8607_ID_LDO4, "ldo-04", IORESOURCE_IO,},
  42. {PM8607_ID_LDO5, PM8607_ID_LDO5, "ldo-05", IORESOURCE_IO,},
  43. {PM8607_ID_LDO6, PM8607_ID_LDO6, "ldo-06", IORESOURCE_IO,},
  44. {PM8607_ID_LDO7, PM8607_ID_LDO7, "ldo-07", IORESOURCE_IO,},
  45. {PM8607_ID_LDO8, PM8607_ID_LDO8, "ldo-08", IORESOURCE_IO,},
  46. {PM8607_ID_LDO9, PM8607_ID_LDO9, "ldo-09", IORESOURCE_IO,},
  47. {PM8607_ID_LDO10, PM8607_ID_LDO10, "ldo-10", IORESOURCE_IO,},
  48. {PM8607_ID_LDO11, PM8607_ID_LDO11, "ldo-11", IORESOURCE_IO,},
  49. {PM8607_ID_LDO12, PM8607_ID_LDO12, "ldo-12", IORESOURCE_IO,},
  50. {PM8607_ID_LDO13, PM8607_ID_LDO13, "ldo-13", IORESOURCE_IO,},
  51. {PM8607_ID_LDO14, PM8607_ID_LDO14, "ldo-14", IORESOURCE_IO,},
  52. {PM8607_ID_LDO15, PM8607_ID_LDO15, "ldo-15", IORESOURCE_IO,},
  53. };
  54. static struct resource touch_resources[] __devinitdata = {
  55. {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
  56. };
  57. static struct resource onkey_resources[] __devinitdata = {
  58. {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
  59. };
  60. static struct resource codec_resources[] __devinitdata = {
  61. /* Headset microphone insertion or removal */
  62. {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
  63. /* Hook-switch press or release */
  64. {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
  65. /* Headset insertion or removal */
  66. {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
  67. /* Audio short */
  68. {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
  69. };
  70. static struct resource battery_resources[] __devinitdata = {
  71. {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
  72. {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
  73. };
  74. static struct resource charger_resources[] __devinitdata = {
  75. {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
  76. {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,},
  77. {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout", IORESOURCE_IRQ,},
  78. {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,},
  79. {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
  80. {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
  81. };
  82. static struct resource preg_resources[] __devinitdata = {
  83. {PM8606_ID_PREG, PM8606_ID_PREG, "preg", IORESOURCE_IO,},
  84. };
  85. static struct resource rtc_resources[] __devinitdata = {
  86. {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
  87. };
  88. static struct mfd_cell bk_devs[] = {
  89. {"88pm860x-backlight", 0,},
  90. {"88pm860x-backlight", 1,},
  91. {"88pm860x-backlight", 2,},
  92. };
  93. static struct mfd_cell led_devs[] = {
  94. {"88pm860x-led", 0,},
  95. {"88pm860x-led", 1,},
  96. {"88pm860x-led", 2,},
  97. {"88pm860x-led", 3,},
  98. {"88pm860x-led", 4,},
  99. {"88pm860x-led", 5,},
  100. };
  101. static struct mfd_cell regulator_devs[] = {
  102. {"88pm860x-regulator", 0,},
  103. {"88pm860x-regulator", 1,},
  104. {"88pm860x-regulator", 2,},
  105. {"88pm860x-regulator", 3,},
  106. {"88pm860x-regulator", 4,},
  107. {"88pm860x-regulator", 5,},
  108. {"88pm860x-regulator", 6,},
  109. {"88pm860x-regulator", 7,},
  110. {"88pm860x-regulator", 8,},
  111. {"88pm860x-regulator", 9,},
  112. {"88pm860x-regulator", 10,},
  113. {"88pm860x-regulator", 11,},
  114. {"88pm860x-regulator", 12,},
  115. {"88pm860x-regulator", 13,},
  116. {"88pm860x-regulator", 14,},
  117. {"88pm860x-regulator", 15,},
  118. {"88pm860x-regulator", 16,},
  119. {"88pm860x-regulator", 17,},
  120. };
  121. static struct mfd_cell touch_devs[] = {
  122. {"88pm860x-touch", -1,},
  123. };
  124. static struct mfd_cell onkey_devs[] = {
  125. {"88pm860x-onkey", -1,},
  126. };
  127. static struct mfd_cell codec_devs[] = {
  128. {"88pm860x-codec", -1,},
  129. };
  130. static struct regulator_consumer_supply preg_supply[] = {
  131. REGULATOR_SUPPLY("preg", "charger-manager"),
  132. };
  133. static struct regulator_init_data preg_init_data = {
  134. .num_consumer_supplies = ARRAY_SIZE(preg_supply),
  135. .consumer_supplies = &preg_supply[0],
  136. };
  137. static struct mfd_cell power_devs[] = {
  138. {"88pm860x-battery", -1,},
  139. {"88pm860x-charger", -1,},
  140. {"88pm860x-preg", -1,},
  141. };
  142. static struct mfd_cell rtc_devs[] = {
  143. {"88pm860x-rtc", -1,},
  144. };
  145. struct pm860x_irq_data {
  146. int reg;
  147. int mask_reg;
  148. int enable; /* enable or not */
  149. int offs; /* bit offset in mask register */
  150. };
  151. static struct pm860x_irq_data pm860x_irqs[] = {
  152. [PM8607_IRQ_ONKEY] = {
  153. .reg = PM8607_INT_STATUS1,
  154. .mask_reg = PM8607_INT_MASK_1,
  155. .offs = 1 << 0,
  156. },
  157. [PM8607_IRQ_EXTON] = {
  158. .reg = PM8607_INT_STATUS1,
  159. .mask_reg = PM8607_INT_MASK_1,
  160. .offs = 1 << 1,
  161. },
  162. [PM8607_IRQ_CHG] = {
  163. .reg = PM8607_INT_STATUS1,
  164. .mask_reg = PM8607_INT_MASK_1,
  165. .offs = 1 << 2,
  166. },
  167. [PM8607_IRQ_BAT] = {
  168. .reg = PM8607_INT_STATUS1,
  169. .mask_reg = PM8607_INT_MASK_1,
  170. .offs = 1 << 3,
  171. },
  172. [PM8607_IRQ_RTC] = {
  173. .reg = PM8607_INT_STATUS1,
  174. .mask_reg = PM8607_INT_MASK_1,
  175. .offs = 1 << 4,
  176. },
  177. [PM8607_IRQ_CC] = {
  178. .reg = PM8607_INT_STATUS1,
  179. .mask_reg = PM8607_INT_MASK_1,
  180. .offs = 1 << 5,
  181. },
  182. [PM8607_IRQ_VBAT] = {
  183. .reg = PM8607_INT_STATUS2,
  184. .mask_reg = PM8607_INT_MASK_2,
  185. .offs = 1 << 0,
  186. },
  187. [PM8607_IRQ_VCHG] = {
  188. .reg = PM8607_INT_STATUS2,
  189. .mask_reg = PM8607_INT_MASK_2,
  190. .offs = 1 << 1,
  191. },
  192. [PM8607_IRQ_VSYS] = {
  193. .reg = PM8607_INT_STATUS2,
  194. .mask_reg = PM8607_INT_MASK_2,
  195. .offs = 1 << 2,
  196. },
  197. [PM8607_IRQ_TINT] = {
  198. .reg = PM8607_INT_STATUS2,
  199. .mask_reg = PM8607_INT_MASK_2,
  200. .offs = 1 << 3,
  201. },
  202. [PM8607_IRQ_GPADC0] = {
  203. .reg = PM8607_INT_STATUS2,
  204. .mask_reg = PM8607_INT_MASK_2,
  205. .offs = 1 << 4,
  206. },
  207. [PM8607_IRQ_GPADC1] = {
  208. .reg = PM8607_INT_STATUS2,
  209. .mask_reg = PM8607_INT_MASK_2,
  210. .offs = 1 << 5,
  211. },
  212. [PM8607_IRQ_GPADC2] = {
  213. .reg = PM8607_INT_STATUS2,
  214. .mask_reg = PM8607_INT_MASK_2,
  215. .offs = 1 << 6,
  216. },
  217. [PM8607_IRQ_GPADC3] = {
  218. .reg = PM8607_INT_STATUS2,
  219. .mask_reg = PM8607_INT_MASK_2,
  220. .offs = 1 << 7,
  221. },
  222. [PM8607_IRQ_AUDIO_SHORT] = {
  223. .reg = PM8607_INT_STATUS3,
  224. .mask_reg = PM8607_INT_MASK_3,
  225. .offs = 1 << 0,
  226. },
  227. [PM8607_IRQ_PEN] = {
  228. .reg = PM8607_INT_STATUS3,
  229. .mask_reg = PM8607_INT_MASK_3,
  230. .offs = 1 << 1,
  231. },
  232. [PM8607_IRQ_HEADSET] = {
  233. .reg = PM8607_INT_STATUS3,
  234. .mask_reg = PM8607_INT_MASK_3,
  235. .offs = 1 << 2,
  236. },
  237. [PM8607_IRQ_HOOK] = {
  238. .reg = PM8607_INT_STATUS3,
  239. .mask_reg = PM8607_INT_MASK_3,
  240. .offs = 1 << 3,
  241. },
  242. [PM8607_IRQ_MICIN] = {
  243. .reg = PM8607_INT_STATUS3,
  244. .mask_reg = PM8607_INT_MASK_3,
  245. .offs = 1 << 4,
  246. },
  247. [PM8607_IRQ_CHG_FAIL] = {
  248. .reg = PM8607_INT_STATUS3,
  249. .mask_reg = PM8607_INT_MASK_3,
  250. .offs = 1 << 5,
  251. },
  252. [PM8607_IRQ_CHG_DONE] = {
  253. .reg = PM8607_INT_STATUS3,
  254. .mask_reg = PM8607_INT_MASK_3,
  255. .offs = 1 << 6,
  256. },
  257. [PM8607_IRQ_CHG_FAULT] = {
  258. .reg = PM8607_INT_STATUS3,
  259. .mask_reg = PM8607_INT_MASK_3,
  260. .offs = 1 << 7,
  261. },
  262. };
  263. static irqreturn_t pm860x_irq(int irq, void *data)
  264. {
  265. struct pm860x_chip *chip = data;
  266. struct pm860x_irq_data *irq_data;
  267. struct i2c_client *i2c;
  268. int read_reg = -1, value = 0;
  269. int i;
  270. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  271. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  272. irq_data = &pm860x_irqs[i];
  273. if (read_reg != irq_data->reg) {
  274. read_reg = irq_data->reg;
  275. value = pm860x_reg_read(i2c, irq_data->reg);
  276. }
  277. if (value & irq_data->enable)
  278. handle_nested_irq(chip->irq_base + i);
  279. }
  280. return IRQ_HANDLED;
  281. }
  282. static void pm860x_irq_lock(struct irq_data *data)
  283. {
  284. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  285. mutex_lock(&chip->irq_lock);
  286. }
  287. static void pm860x_irq_sync_unlock(struct irq_data *data)
  288. {
  289. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  290. struct pm860x_irq_data *irq_data;
  291. struct i2c_client *i2c;
  292. static unsigned char cached[3] = {0x0, 0x0, 0x0};
  293. unsigned char mask[3];
  294. int i;
  295. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  296. /* Load cached value. In initial, all IRQs are masked */
  297. for (i = 0; i < 3; i++)
  298. mask[i] = cached[i];
  299. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  300. irq_data = &pm860x_irqs[i];
  301. switch (irq_data->mask_reg) {
  302. case PM8607_INT_MASK_1:
  303. mask[0] &= ~irq_data->offs;
  304. mask[0] |= irq_data->enable;
  305. break;
  306. case PM8607_INT_MASK_2:
  307. mask[1] &= ~irq_data->offs;
  308. mask[1] |= irq_data->enable;
  309. break;
  310. case PM8607_INT_MASK_3:
  311. mask[2] &= ~irq_data->offs;
  312. mask[2] |= irq_data->enable;
  313. break;
  314. default:
  315. dev_err(chip->dev, "wrong IRQ\n");
  316. break;
  317. }
  318. }
  319. /* update mask into registers */
  320. for (i = 0; i < 3; i++) {
  321. if (mask[i] != cached[i]) {
  322. cached[i] = mask[i];
  323. pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
  324. }
  325. }
  326. mutex_unlock(&chip->irq_lock);
  327. }
  328. static void pm860x_irq_enable(struct irq_data *data)
  329. {
  330. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  331. pm860x_irqs[data->irq - chip->irq_base].enable
  332. = pm860x_irqs[data->irq - chip->irq_base].offs;
  333. }
  334. static void pm860x_irq_disable(struct irq_data *data)
  335. {
  336. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  337. pm860x_irqs[data->irq - chip->irq_base].enable = 0;
  338. }
  339. static struct irq_chip pm860x_irq_chip = {
  340. .name = "88pm860x",
  341. .irq_bus_lock = pm860x_irq_lock,
  342. .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
  343. .irq_enable = pm860x_irq_enable,
  344. .irq_disable = pm860x_irq_disable,
  345. };
  346. static int __devinit device_gpadc_init(struct pm860x_chip *chip,
  347. struct pm860x_platform_data *pdata)
  348. {
  349. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  350. : chip->companion;
  351. int data;
  352. int ret;
  353. /* initialize GPADC without activating it */
  354. if (!pdata || !pdata->touch)
  355. return -EINVAL;
  356. /* set GPADC MISC1 register */
  357. data = 0;
  358. data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
  359. data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  360. data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  361. data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
  362. if (data) {
  363. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  364. if (ret < 0)
  365. goto out;
  366. }
  367. /* set tsi prebias time */
  368. if (pdata->touch->tsi_prebias) {
  369. data = pdata->touch->tsi_prebias;
  370. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  371. if (ret < 0)
  372. goto out;
  373. }
  374. /* set prebias & prechg time of pen detect */
  375. data = 0;
  376. data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
  377. data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
  378. if (data) {
  379. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  380. if (ret < 0)
  381. goto out;
  382. }
  383. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
  384. PM8607_GPADC_EN, PM8607_GPADC_EN);
  385. out:
  386. return ret;
  387. }
  388. static int __devinit device_irq_init(struct pm860x_chip *chip,
  389. struct pm860x_platform_data *pdata)
  390. {
  391. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  392. : chip->companion;
  393. unsigned char status_buf[INT_STATUS_NUM];
  394. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  395. int i, data, mask, ret = -EINVAL;
  396. int __irq;
  397. if (!pdata || !pdata->irq_base) {
  398. dev_warn(chip->dev, "No interrupt support on IRQ base\n");
  399. return -EINVAL;
  400. }
  401. mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
  402. | PM8607_B0_MISC1_INT_MASK;
  403. data = 0;
  404. chip->irq_mode = 0;
  405. if (pdata && pdata->irq_mode) {
  406. /*
  407. * irq_mode defines the way of clearing interrupt. If it's 1,
  408. * clear IRQ by write. Otherwise, clear it by read.
  409. * This control bit is valid from 88PM8607 B0 steping.
  410. */
  411. data |= PM8607_B0_MISC1_INT_CLEAR;
  412. chip->irq_mode = 1;
  413. }
  414. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
  415. if (ret < 0)
  416. goto out;
  417. /* mask all IRQs */
  418. memset(status_buf, 0, INT_STATUS_NUM);
  419. ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
  420. INT_STATUS_NUM, status_buf);
  421. if (ret < 0)
  422. goto out;
  423. if (chip->irq_mode) {
  424. /* clear interrupt status by write */
  425. memset(status_buf, 0xFF, INT_STATUS_NUM);
  426. ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
  427. INT_STATUS_NUM, status_buf);
  428. } else {
  429. /* clear interrupt status by read */
  430. ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
  431. INT_STATUS_NUM, status_buf);
  432. }
  433. if (ret < 0)
  434. goto out;
  435. mutex_init(&chip->irq_lock);
  436. chip->irq_base = pdata->irq_base;
  437. chip->core_irq = i2c->irq;
  438. if (!chip->core_irq)
  439. goto out;
  440. /* register IRQ by genirq */
  441. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  442. __irq = i + chip->irq_base;
  443. irq_set_chip_data(__irq, chip);
  444. irq_set_chip_and_handler(__irq, &pm860x_irq_chip,
  445. handle_edge_irq);
  446. irq_set_nested_thread(__irq, 1);
  447. #ifdef CONFIG_ARM
  448. set_irq_flags(__irq, IRQF_VALID);
  449. #else
  450. irq_set_noprobe(__irq);
  451. #endif
  452. }
  453. ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags,
  454. "88pm860x", chip);
  455. if (ret) {
  456. dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
  457. chip->core_irq = 0;
  458. }
  459. return 0;
  460. out:
  461. chip->core_irq = 0;
  462. return ret;
  463. }
  464. static void device_irq_exit(struct pm860x_chip *chip)
  465. {
  466. if (chip->core_irq)
  467. free_irq(chip->core_irq, chip);
  468. }
  469. int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
  470. {
  471. int ret = -EIO;
  472. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  473. chip->client : chip->companion;
  474. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  475. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  476. __func__, chip->osc_vote,
  477. chip->osc_status);
  478. mutex_lock(&chip->osc_lock);
  479. /* Update voting status */
  480. chip->osc_vote |= client;
  481. /* If reference group is off - turn on*/
  482. if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
  483. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  484. /* Enable Reference group Vsys */
  485. if (pm860x_set_bits(i2c, PM8606_VSYS,
  486. PM8606_VSYS_EN, PM8606_VSYS_EN))
  487. goto out;
  488. /*Enable Internal Oscillator */
  489. if (pm860x_set_bits(i2c, PM8606_MISC,
  490. PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
  491. goto out;
  492. /* Update status (only if writes succeed) */
  493. chip->osc_status = PM8606_REF_GP_OSC_ON;
  494. }
  495. mutex_unlock(&chip->osc_lock);
  496. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  497. __func__, chip->osc_vote,
  498. chip->osc_status, ret);
  499. return 0;
  500. out:
  501. mutex_unlock(&chip->osc_lock);
  502. return ret;
  503. }
  504. EXPORT_SYMBOL(pm8606_osc_enable);
  505. int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
  506. {
  507. int ret = -EIO;
  508. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  509. chip->client : chip->companion;
  510. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  511. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  512. __func__, chip->osc_vote,
  513. chip->osc_status);
  514. mutex_lock(&chip->osc_lock);
  515. /*Update voting status */
  516. chip->osc_vote &= ~(client);
  517. /* If reference group is off and this is the last client to release
  518. * - turn off */
  519. if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
  520. (chip->osc_vote == REF_GP_NO_CLIENTS)) {
  521. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  522. /* Disable Reference group Vsys */
  523. if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
  524. goto out;
  525. /* Disable Internal Oscillator */
  526. if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
  527. goto out;
  528. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  529. }
  530. mutex_unlock(&chip->osc_lock);
  531. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  532. __func__, chip->osc_vote,
  533. chip->osc_status, ret);
  534. return 0;
  535. out:
  536. mutex_unlock(&chip->osc_lock);
  537. return ret;
  538. }
  539. EXPORT_SYMBOL(pm8606_osc_disable);
  540. static void __devinit device_osc_init(struct i2c_client *i2c)
  541. {
  542. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  543. mutex_init(&chip->osc_lock);
  544. /* init portofino reference group voting and status */
  545. /* Disable Reference group Vsys */
  546. pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
  547. /* Disable Internal Oscillator */
  548. pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
  549. chip->osc_vote = REF_GP_NO_CLIENTS;
  550. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  551. }
  552. static void __devinit device_bk_init(struct pm860x_chip *chip,
  553. struct pm860x_platform_data *pdata)
  554. {
  555. int ret;
  556. int i, j, id;
  557. if ((pdata == NULL) || (pdata->backlight == NULL))
  558. return;
  559. if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
  560. pdata->num_backlights = ARRAY_SIZE(bk_devs);
  561. for (i = 0; i < pdata->num_backlights; i++) {
  562. bk_devs[i].platform_data = &pdata->backlight[i];
  563. bk_devs[i].pdata_size = sizeof(struct pm860x_backlight_pdata);
  564. for (j = 0; j < ARRAY_SIZE(bk_devs); j++) {
  565. id = bk_resources[j].start;
  566. if (pdata->backlight[i].flags != id)
  567. continue;
  568. bk_devs[i].num_resources = 1;
  569. bk_devs[i].resources = &bk_resources[j];
  570. ret = mfd_add_devices(chip->dev, 0,
  571. &bk_devs[i], 1,
  572. &bk_resources[j], 0);
  573. if (ret < 0) {
  574. dev_err(chip->dev, "Failed to add "
  575. "backlight subdev\n");
  576. return;
  577. }
  578. }
  579. }
  580. }
  581. static void __devinit device_led_init(struct pm860x_chip *chip,
  582. struct pm860x_platform_data *pdata)
  583. {
  584. int ret;
  585. int i, j, id;
  586. if ((pdata == NULL) || (pdata->led == NULL))
  587. return;
  588. if (pdata->num_leds > ARRAY_SIZE(led_devs))
  589. pdata->num_leds = ARRAY_SIZE(led_devs);
  590. for (i = 0; i < pdata->num_leds; i++) {
  591. led_devs[i].platform_data = &pdata->led[i];
  592. led_devs[i].pdata_size = sizeof(struct pm860x_led_pdata);
  593. for (j = 0; j < ARRAY_SIZE(led_devs); j++) {
  594. id = led_resources[j].start;
  595. if (pdata->led[i].flags != id)
  596. continue;
  597. led_devs[i].num_resources = 1;
  598. led_devs[i].resources = &led_resources[j],
  599. ret = mfd_add_devices(chip->dev, 0,
  600. &led_devs[i], 1,
  601. &led_resources[j], 0);
  602. if (ret < 0) {
  603. dev_err(chip->dev, "Failed to add "
  604. "led subdev\n");
  605. return;
  606. }
  607. }
  608. }
  609. }
  610. static void __devinit device_regulator_init(struct pm860x_chip *chip,
  611. struct pm860x_platform_data *pdata)
  612. {
  613. struct regulator_init_data *initdata;
  614. int ret;
  615. int i, seq;
  616. if ((pdata == NULL) || (pdata->regulator == NULL))
  617. return;
  618. if (pdata->num_regulators > ARRAY_SIZE(regulator_devs))
  619. pdata->num_regulators = ARRAY_SIZE(regulator_devs);
  620. for (i = 0, seq = -1; i < pdata->num_regulators; i++) {
  621. initdata = &pdata->regulator[i];
  622. seq = *(unsigned int *)initdata->driver_data;
  623. if ((seq < 0) || (seq > PM8607_ID_RG_MAX)) {
  624. dev_err(chip->dev, "Wrong ID(%d) on regulator(%s)\n",
  625. seq, initdata->constraints.name);
  626. goto out;
  627. }
  628. regulator_devs[i].platform_data = &pdata->regulator[i];
  629. regulator_devs[i].pdata_size = sizeof(struct regulator_init_data);
  630. regulator_devs[i].num_resources = 1;
  631. regulator_devs[i].resources = &regulator_resources[seq];
  632. ret = mfd_add_devices(chip->dev, 0, &regulator_devs[i], 1,
  633. &regulator_resources[seq], 0);
  634. if (ret < 0) {
  635. dev_err(chip->dev, "Failed to add regulator subdev\n");
  636. goto out;
  637. }
  638. }
  639. out:
  640. return;
  641. }
  642. static void __devinit device_rtc_init(struct pm860x_chip *chip,
  643. struct pm860x_platform_data *pdata)
  644. {
  645. int ret;
  646. if ((pdata == NULL))
  647. return;
  648. rtc_devs[0].platform_data = pdata->rtc;
  649. rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
  650. rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
  651. rtc_devs[0].resources = &rtc_resources[0];
  652. ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
  653. ARRAY_SIZE(rtc_devs), &rtc_resources[0],
  654. chip->irq_base);
  655. if (ret < 0)
  656. dev_err(chip->dev, "Failed to add rtc subdev\n");
  657. }
  658. static void __devinit device_touch_init(struct pm860x_chip *chip,
  659. struct pm860x_platform_data *pdata)
  660. {
  661. int ret;
  662. if (pdata == NULL)
  663. return;
  664. touch_devs[0].platform_data = pdata->touch;
  665. touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
  666. touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
  667. touch_devs[0].resources = &touch_resources[0];
  668. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  669. ARRAY_SIZE(touch_devs), &touch_resources[0],
  670. chip->irq_base);
  671. if (ret < 0)
  672. dev_err(chip->dev, "Failed to add touch subdev\n");
  673. }
  674. static void __devinit device_power_init(struct pm860x_chip *chip,
  675. struct pm860x_platform_data *pdata)
  676. {
  677. int ret;
  678. if (pdata == NULL)
  679. return;
  680. power_devs[0].platform_data = pdata->power;
  681. power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
  682. power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
  683. power_devs[0].resources = &battery_resources[0],
  684. ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
  685. &battery_resources[0], chip->irq_base);
  686. if (ret < 0)
  687. dev_err(chip->dev, "Failed to add battery subdev\n");
  688. power_devs[1].platform_data = pdata->power;
  689. power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
  690. power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
  691. power_devs[1].resources = &charger_resources[0],
  692. ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
  693. &charger_resources[0], chip->irq_base);
  694. if (ret < 0)
  695. dev_err(chip->dev, "Failed to add charger subdev\n");
  696. power_devs[2].platform_data = &preg_init_data;
  697. power_devs[2].pdata_size = sizeof(struct regulator_init_data);
  698. power_devs[2].num_resources = ARRAY_SIZE(preg_resources);
  699. power_devs[2].resources = &preg_resources[0],
  700. ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
  701. &preg_resources[0], chip->irq_base);
  702. if (ret < 0)
  703. dev_err(chip->dev, "Failed to add preg subdev\n");
  704. }
  705. static void __devinit device_onkey_init(struct pm860x_chip *chip,
  706. struct pm860x_platform_data *pdata)
  707. {
  708. int ret;
  709. onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
  710. onkey_devs[0].resources = &onkey_resources[0],
  711. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  712. ARRAY_SIZE(onkey_devs), &onkey_resources[0],
  713. chip->irq_base);
  714. if (ret < 0)
  715. dev_err(chip->dev, "Failed to add onkey subdev\n");
  716. }
  717. static void __devinit device_codec_init(struct pm860x_chip *chip,
  718. struct pm860x_platform_data *pdata)
  719. {
  720. int ret;
  721. codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
  722. codec_devs[0].resources = &codec_resources[0],
  723. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  724. ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
  725. if (ret < 0)
  726. dev_err(chip->dev, "Failed to add codec subdev\n");
  727. }
  728. static void __devinit device_8607_init(struct pm860x_chip *chip,
  729. struct i2c_client *i2c,
  730. struct pm860x_platform_data *pdata)
  731. {
  732. int data, ret;
  733. ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
  734. if (ret < 0) {
  735. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  736. goto out;
  737. }
  738. switch (ret & PM8607_VERSION_MASK) {
  739. case 0x40:
  740. case 0x50:
  741. dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
  742. ret);
  743. break;
  744. default:
  745. dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
  746. "Chip ID: %02x\n", ret);
  747. goto out;
  748. }
  749. ret = pm860x_reg_read(i2c, PM8607_BUCK3);
  750. if (ret < 0) {
  751. dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
  752. goto out;
  753. }
  754. if (ret & PM8607_BUCK3_DOUBLE)
  755. chip->buck3_double = 1;
  756. ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
  757. if (ret < 0) {
  758. dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
  759. goto out;
  760. }
  761. if (pdata && (pdata->i2c_port == PI2C_PORT))
  762. data = PM8607_B0_MISC1_PI2C;
  763. else
  764. data = 0;
  765. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
  766. if (ret < 0) {
  767. dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
  768. goto out;
  769. }
  770. ret = device_gpadc_init(chip, pdata);
  771. if (ret < 0)
  772. goto out;
  773. ret = device_irq_init(chip, pdata);
  774. if (ret < 0)
  775. goto out;
  776. device_regulator_init(chip, pdata);
  777. device_rtc_init(chip, pdata);
  778. device_onkey_init(chip, pdata);
  779. device_touch_init(chip, pdata);
  780. device_power_init(chip, pdata);
  781. device_codec_init(chip, pdata);
  782. out:
  783. return;
  784. }
  785. static void __devinit device_8606_init(struct pm860x_chip *chip,
  786. struct i2c_client *i2c,
  787. struct pm860x_platform_data *pdata)
  788. {
  789. device_osc_init(i2c);
  790. device_bk_init(chip, pdata);
  791. device_led_init(chip, pdata);
  792. }
  793. int __devinit pm860x_device_init(struct pm860x_chip *chip,
  794. struct pm860x_platform_data *pdata)
  795. {
  796. chip->core_irq = 0;
  797. switch (chip->id) {
  798. case CHIP_PM8606:
  799. device_8606_init(chip, chip->client, pdata);
  800. break;
  801. case CHIP_PM8607:
  802. device_8607_init(chip, chip->client, pdata);
  803. break;
  804. }
  805. if (chip->companion) {
  806. switch (chip->id) {
  807. case CHIP_PM8607:
  808. device_8606_init(chip, chip->companion, pdata);
  809. break;
  810. case CHIP_PM8606:
  811. device_8607_init(chip, chip->companion, pdata);
  812. break;
  813. }
  814. }
  815. return 0;
  816. }
  817. void __devexit pm860x_device_exit(struct pm860x_chip *chip)
  818. {
  819. device_irq_exit(chip);
  820. mfd_remove_devices(chip->dev);
  821. }
  822. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
  823. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  824. MODULE_LICENSE("GPL");