88pm860x-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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[] __initdata = {
  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[] __initdata = {
  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[] __initdata = {
  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[] __initdata = {
  55. {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
  56. };
  57. static struct resource onkey_resources[] __initdata = {
  58. {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
  59. };
  60. static struct resource codec_resources[] __initdata = {
  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[] __initdata = {
  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[] __initdata = {
  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 mfd_cell bk_devs[] __initdata = {
  83. {"88pm860x-backlight", 0,},
  84. {"88pm860x-backlight", 1,},
  85. {"88pm860x-backlight", 2,},
  86. };
  87. static struct mfd_cell led_devs[] __initdata = {
  88. {"88pm860x-led", 0,},
  89. {"88pm860x-led", 1,},
  90. {"88pm860x-led", 2,},
  91. {"88pm860x-led", 3,},
  92. {"88pm860x-led", 4,},
  93. {"88pm860x-led", 5,},
  94. };
  95. static struct mfd_cell regulator_devs[] __initdata = {
  96. {"88pm860x-regulator", 0,},
  97. {"88pm860x-regulator", 1,},
  98. {"88pm860x-regulator", 2,},
  99. {"88pm860x-regulator", 3,},
  100. {"88pm860x-regulator", 4,},
  101. {"88pm860x-regulator", 5,},
  102. {"88pm860x-regulator", 6,},
  103. {"88pm860x-regulator", 7,},
  104. {"88pm860x-regulator", 8,},
  105. {"88pm860x-regulator", 9,},
  106. {"88pm860x-regulator", 10,},
  107. {"88pm860x-regulator", 11,},
  108. {"88pm860x-regulator", 12,},
  109. {"88pm860x-regulator", 13,},
  110. {"88pm860x-regulator", 14,},
  111. {"88pm860x-regulator", 15,},
  112. {"88pm860x-regulator", 16,},
  113. {"88pm860x-regulator", 17,},
  114. };
  115. static struct mfd_cell touch_devs[] __initdata = {
  116. {"88pm860x-touch", -1,},
  117. };
  118. static struct mfd_cell onkey_devs[] __initdata = {
  119. {"88pm860x-onkey", -1,},
  120. };
  121. static struct mfd_cell codec_devs[] __initdata = {
  122. {"88pm860x-codec", -1,},
  123. };
  124. static struct mfd_cell power_devs[] = {
  125. {"88pm860x-battery", -1,},
  126. {"88pm860x-charger", -1,},
  127. };
  128. static struct pm860x_backlight_pdata bk_pdata[ARRAY_SIZE(bk_devs)];
  129. static struct pm860x_led_pdata led_pdata[ARRAY_SIZE(led_devs)];
  130. static struct regulator_init_data regulator_pdata[ARRAY_SIZE(regulator_devs)];
  131. static struct pm860x_touch_pdata touch_pdata;
  132. static struct pm860x_power_pdata power_pdata;
  133. struct pm860x_irq_data {
  134. int reg;
  135. int mask_reg;
  136. int enable; /* enable or not */
  137. int offs; /* bit offset in mask register */
  138. };
  139. static struct pm860x_irq_data pm860x_irqs[] = {
  140. [PM8607_IRQ_ONKEY] = {
  141. .reg = PM8607_INT_STATUS1,
  142. .mask_reg = PM8607_INT_MASK_1,
  143. .offs = 1 << 0,
  144. },
  145. [PM8607_IRQ_EXTON] = {
  146. .reg = PM8607_INT_STATUS1,
  147. .mask_reg = PM8607_INT_MASK_1,
  148. .offs = 1 << 1,
  149. },
  150. [PM8607_IRQ_CHG] = {
  151. .reg = PM8607_INT_STATUS1,
  152. .mask_reg = PM8607_INT_MASK_1,
  153. .offs = 1 << 2,
  154. },
  155. [PM8607_IRQ_BAT] = {
  156. .reg = PM8607_INT_STATUS1,
  157. .mask_reg = PM8607_INT_MASK_1,
  158. .offs = 1 << 3,
  159. },
  160. [PM8607_IRQ_RTC] = {
  161. .reg = PM8607_INT_STATUS1,
  162. .mask_reg = PM8607_INT_MASK_1,
  163. .offs = 1 << 4,
  164. },
  165. [PM8607_IRQ_CC] = {
  166. .reg = PM8607_INT_STATUS1,
  167. .mask_reg = PM8607_INT_MASK_1,
  168. .offs = 1 << 5,
  169. },
  170. [PM8607_IRQ_VBAT] = {
  171. .reg = PM8607_INT_STATUS2,
  172. .mask_reg = PM8607_INT_MASK_2,
  173. .offs = 1 << 0,
  174. },
  175. [PM8607_IRQ_VCHG] = {
  176. .reg = PM8607_INT_STATUS2,
  177. .mask_reg = PM8607_INT_MASK_2,
  178. .offs = 1 << 1,
  179. },
  180. [PM8607_IRQ_VSYS] = {
  181. .reg = PM8607_INT_STATUS2,
  182. .mask_reg = PM8607_INT_MASK_2,
  183. .offs = 1 << 2,
  184. },
  185. [PM8607_IRQ_TINT] = {
  186. .reg = PM8607_INT_STATUS2,
  187. .mask_reg = PM8607_INT_MASK_2,
  188. .offs = 1 << 3,
  189. },
  190. [PM8607_IRQ_GPADC0] = {
  191. .reg = PM8607_INT_STATUS2,
  192. .mask_reg = PM8607_INT_MASK_2,
  193. .offs = 1 << 4,
  194. },
  195. [PM8607_IRQ_GPADC1] = {
  196. .reg = PM8607_INT_STATUS2,
  197. .mask_reg = PM8607_INT_MASK_2,
  198. .offs = 1 << 5,
  199. },
  200. [PM8607_IRQ_GPADC2] = {
  201. .reg = PM8607_INT_STATUS2,
  202. .mask_reg = PM8607_INT_MASK_2,
  203. .offs = 1 << 6,
  204. },
  205. [PM8607_IRQ_GPADC3] = {
  206. .reg = PM8607_INT_STATUS2,
  207. .mask_reg = PM8607_INT_MASK_2,
  208. .offs = 1 << 7,
  209. },
  210. [PM8607_IRQ_AUDIO_SHORT] = {
  211. .reg = PM8607_INT_STATUS3,
  212. .mask_reg = PM8607_INT_MASK_3,
  213. .offs = 1 << 0,
  214. },
  215. [PM8607_IRQ_PEN] = {
  216. .reg = PM8607_INT_STATUS3,
  217. .mask_reg = PM8607_INT_MASK_3,
  218. .offs = 1 << 1,
  219. },
  220. [PM8607_IRQ_HEADSET] = {
  221. .reg = PM8607_INT_STATUS3,
  222. .mask_reg = PM8607_INT_MASK_3,
  223. .offs = 1 << 2,
  224. },
  225. [PM8607_IRQ_HOOK] = {
  226. .reg = PM8607_INT_STATUS3,
  227. .mask_reg = PM8607_INT_MASK_3,
  228. .offs = 1 << 3,
  229. },
  230. [PM8607_IRQ_MICIN] = {
  231. .reg = PM8607_INT_STATUS3,
  232. .mask_reg = PM8607_INT_MASK_3,
  233. .offs = 1 << 4,
  234. },
  235. [PM8607_IRQ_CHG_FAIL] = {
  236. .reg = PM8607_INT_STATUS3,
  237. .mask_reg = PM8607_INT_MASK_3,
  238. .offs = 1 << 5,
  239. },
  240. [PM8607_IRQ_CHG_DONE] = {
  241. .reg = PM8607_INT_STATUS3,
  242. .mask_reg = PM8607_INT_MASK_3,
  243. .offs = 1 << 6,
  244. },
  245. [PM8607_IRQ_CHG_FAULT] = {
  246. .reg = PM8607_INT_STATUS3,
  247. .mask_reg = PM8607_INT_MASK_3,
  248. .offs = 1 << 7,
  249. },
  250. };
  251. static irqreturn_t pm860x_irq(int irq, void *data)
  252. {
  253. struct pm860x_chip *chip = data;
  254. struct pm860x_irq_data *irq_data;
  255. struct i2c_client *i2c;
  256. int read_reg = -1, value = 0;
  257. int i;
  258. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  259. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  260. irq_data = &pm860x_irqs[i];
  261. if (read_reg != irq_data->reg) {
  262. read_reg = irq_data->reg;
  263. value = pm860x_reg_read(i2c, irq_data->reg);
  264. }
  265. if (value & irq_data->enable)
  266. handle_nested_irq(chip->irq_base + i);
  267. }
  268. return IRQ_HANDLED;
  269. }
  270. static void pm860x_irq_lock(struct irq_data *data)
  271. {
  272. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  273. mutex_lock(&chip->irq_lock);
  274. }
  275. static void pm860x_irq_sync_unlock(struct irq_data *data)
  276. {
  277. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  278. struct pm860x_irq_data *irq_data;
  279. struct i2c_client *i2c;
  280. static unsigned char cached[3] = {0x0, 0x0, 0x0};
  281. unsigned char mask[3];
  282. int i;
  283. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  284. /* Load cached value. In initial, all IRQs are masked */
  285. for (i = 0; i < 3; i++)
  286. mask[i] = cached[i];
  287. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  288. irq_data = &pm860x_irqs[i];
  289. switch (irq_data->mask_reg) {
  290. case PM8607_INT_MASK_1:
  291. mask[0] &= ~irq_data->offs;
  292. mask[0] |= irq_data->enable;
  293. break;
  294. case PM8607_INT_MASK_2:
  295. mask[1] &= ~irq_data->offs;
  296. mask[1] |= irq_data->enable;
  297. break;
  298. case PM8607_INT_MASK_3:
  299. mask[2] &= ~irq_data->offs;
  300. mask[2] |= irq_data->enable;
  301. break;
  302. default:
  303. dev_err(chip->dev, "wrong IRQ\n");
  304. break;
  305. }
  306. }
  307. /* update mask into registers */
  308. for (i = 0; i < 3; i++) {
  309. if (mask[i] != cached[i]) {
  310. cached[i] = mask[i];
  311. pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
  312. }
  313. }
  314. mutex_unlock(&chip->irq_lock);
  315. }
  316. static void pm860x_irq_enable(struct irq_data *data)
  317. {
  318. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  319. pm860x_irqs[data->irq - chip->irq_base].enable
  320. = pm860x_irqs[data->irq - chip->irq_base].offs;
  321. }
  322. static void pm860x_irq_disable(struct irq_data *data)
  323. {
  324. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  325. pm860x_irqs[data->irq - chip->irq_base].enable = 0;
  326. }
  327. static struct irq_chip pm860x_irq_chip = {
  328. .name = "88pm860x",
  329. .irq_bus_lock = pm860x_irq_lock,
  330. .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
  331. .irq_enable = pm860x_irq_enable,
  332. .irq_disable = pm860x_irq_disable,
  333. };
  334. static int __devinit device_gpadc_init(struct pm860x_chip *chip,
  335. struct pm860x_platform_data *pdata)
  336. {
  337. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  338. : chip->companion;
  339. int data;
  340. int ret;
  341. /* initialize GPADC without activating it */
  342. if (!pdata || !pdata->touch)
  343. return -EINVAL;
  344. /* set GPADC MISC1 register */
  345. data = 0;
  346. data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
  347. data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  348. data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  349. data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
  350. if (data) {
  351. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  352. if (ret < 0)
  353. goto out;
  354. }
  355. /* set tsi prebias time */
  356. if (pdata->touch->tsi_prebias) {
  357. data = pdata->touch->tsi_prebias;
  358. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  359. if (ret < 0)
  360. goto out;
  361. }
  362. /* set prebias & prechg time of pen detect */
  363. data = 0;
  364. data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
  365. data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
  366. if (data) {
  367. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  368. if (ret < 0)
  369. goto out;
  370. }
  371. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
  372. PM8607_GPADC_EN, PM8607_GPADC_EN);
  373. out:
  374. return ret;
  375. }
  376. static int __devinit device_irq_init(struct pm860x_chip *chip,
  377. struct pm860x_platform_data *pdata)
  378. {
  379. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  380. : chip->companion;
  381. unsigned char status_buf[INT_STATUS_NUM];
  382. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  383. struct irq_desc *desc;
  384. int i, data, mask, ret = -EINVAL;
  385. int __irq;
  386. if (!pdata || !pdata->irq_base) {
  387. dev_warn(chip->dev, "No interrupt support on IRQ base\n");
  388. return -EINVAL;
  389. }
  390. mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
  391. | PM8607_B0_MISC1_INT_MASK;
  392. data = 0;
  393. chip->irq_mode = 0;
  394. if (pdata && pdata->irq_mode) {
  395. /*
  396. * irq_mode defines the way of clearing interrupt. If it's 1,
  397. * clear IRQ by write. Otherwise, clear it by read.
  398. * This control bit is valid from 88PM8607 B0 steping.
  399. */
  400. data |= PM8607_B0_MISC1_INT_CLEAR;
  401. chip->irq_mode = 1;
  402. }
  403. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
  404. if (ret < 0)
  405. goto out;
  406. /* mask all IRQs */
  407. memset(status_buf, 0, INT_STATUS_NUM);
  408. ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
  409. INT_STATUS_NUM, status_buf);
  410. if (ret < 0)
  411. goto out;
  412. if (chip->irq_mode) {
  413. /* clear interrupt status by write */
  414. memset(status_buf, 0xFF, INT_STATUS_NUM);
  415. ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
  416. INT_STATUS_NUM, status_buf);
  417. } else {
  418. /* clear interrupt status by read */
  419. ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
  420. INT_STATUS_NUM, status_buf);
  421. }
  422. if (ret < 0)
  423. goto out;
  424. mutex_init(&chip->irq_lock);
  425. chip->irq_base = pdata->irq_base;
  426. chip->core_irq = i2c->irq;
  427. if (!chip->core_irq)
  428. goto out;
  429. desc = irq_to_desc(chip->core_irq);
  430. /* register IRQ by genirq */
  431. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  432. __irq = i + chip->irq_base;
  433. set_irq_chip_data(__irq, chip);
  434. set_irq_chip_and_handler(__irq, &pm860x_irq_chip,
  435. handle_edge_irq);
  436. set_irq_nested_thread(__irq, 1);
  437. #ifdef CONFIG_ARM
  438. set_irq_flags(__irq, IRQF_VALID);
  439. #else
  440. set_irq_noprobe(__irq);
  441. #endif
  442. }
  443. ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags,
  444. "88pm860x", chip);
  445. if (ret) {
  446. dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
  447. chip->core_irq = 0;
  448. }
  449. return 0;
  450. out:
  451. chip->core_irq = 0;
  452. return ret;
  453. }
  454. static void device_irq_exit(struct pm860x_chip *chip)
  455. {
  456. if (chip->core_irq)
  457. free_irq(chip->core_irq, chip);
  458. }
  459. static void __devinit device_bk_init(struct pm860x_chip *chip,
  460. struct i2c_client *i2c,
  461. struct pm860x_platform_data *pdata)
  462. {
  463. int ret;
  464. int i, j, id;
  465. if ((pdata == NULL) || (pdata->backlight == NULL))
  466. return;
  467. if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
  468. pdata->num_backlights = ARRAY_SIZE(bk_devs);
  469. for (i = 0; i < pdata->num_backlights; i++) {
  470. memcpy(&bk_pdata[i], &pdata->backlight[i],
  471. sizeof(struct pm860x_backlight_pdata));
  472. bk_devs[i].mfd_data = &bk_pdata[i];
  473. for (j = 0; j < ARRAY_SIZE(bk_devs); j++) {
  474. id = bk_resources[j].start;
  475. if (bk_pdata[i].flags != id)
  476. continue;
  477. bk_devs[i].num_resources = 1;
  478. bk_devs[i].resources = &bk_resources[j];
  479. ret = mfd_add_devices(chip->dev, 0,
  480. &bk_devs[i], 1,
  481. &bk_resources[j], 0);
  482. if (ret < 0) {
  483. dev_err(chip->dev, "Failed to add "
  484. "backlight subdev\n");
  485. return;
  486. }
  487. }
  488. }
  489. }
  490. static void __devinit device_led_init(struct pm860x_chip *chip,
  491. struct i2c_client *i2c,
  492. struct pm860x_platform_data *pdata)
  493. {
  494. int ret;
  495. int i, j, id;
  496. if ((pdata == NULL) || (pdata->led == NULL))
  497. return;
  498. if (pdata->num_leds > ARRAY_SIZE(led_devs))
  499. pdata->num_leds = ARRAY_SIZE(led_devs);
  500. for (i = 0; i < pdata->num_leds; i++) {
  501. memcpy(&led_pdata[i], &pdata->led[i],
  502. sizeof(struct pm860x_led_pdata));
  503. led_devs[i].mfd_data = &led_pdata[i];
  504. for (j = 0; j < ARRAY_SIZE(led_devs); j++) {
  505. id = led_resources[j].start;
  506. if (led_pdata[i].flags != id)
  507. continue;
  508. led_devs[i].num_resources = 1;
  509. led_devs[i].resources = &led_resources[j],
  510. ret = mfd_add_devices(chip->dev, 0,
  511. &led_devs[i], 1,
  512. &led_resources[j], 0);
  513. if (ret < 0) {
  514. dev_err(chip->dev, "Failed to add "
  515. "led subdev\n");
  516. return;
  517. }
  518. }
  519. }
  520. }
  521. static void __devinit device_regulator_init(struct pm860x_chip *chip,
  522. struct i2c_client *i2c,
  523. struct pm860x_platform_data *pdata)
  524. {
  525. struct regulator_init_data *initdata;
  526. int ret;
  527. int i, j;
  528. if ((pdata == NULL) || (pdata->regulator == NULL))
  529. return;
  530. if (pdata->num_regulators > ARRAY_SIZE(regulator_devs))
  531. pdata->num_regulators = ARRAY_SIZE(regulator_devs);
  532. for (i = 0, j = -1; i < pdata->num_regulators; i++) {
  533. initdata = &pdata->regulator[i];
  534. if (strstr(initdata->constraints.name, "BUCK")) {
  535. sscanf(initdata->constraints.name, "BUCK%d", &j);
  536. /* BUCK1 ~ BUCK3 */
  537. if ((j < 1) || (j > 3)) {
  538. dev_err(chip->dev, "Failed to add constraint "
  539. "(%s)\n", initdata->constraints.name);
  540. goto out;
  541. }
  542. j = (j - 1) + PM8607_ID_BUCK1;
  543. }
  544. if (strstr(initdata->constraints.name, "LDO")) {
  545. sscanf(initdata->constraints.name, "LDO%d", &j);
  546. /* LDO1 ~ LDO15 */
  547. if ((j < 1) || (j > 15)) {
  548. dev_err(chip->dev, "Failed to add constraint "
  549. "(%s)\n", initdata->constraints.name);
  550. goto out;
  551. }
  552. j = (j - 1) + PM8607_ID_LDO1;
  553. }
  554. if (j == -1) {
  555. dev_err(chip->dev, "Failed to add constraint (%s)\n",
  556. initdata->constraints.name);
  557. goto out;
  558. }
  559. memcpy(&regulator_pdata[i], &pdata->regulator[i],
  560. sizeof(struct regulator_init_data));
  561. regulator_devs[i].mfd_data = &regulator_pdata[i];
  562. regulator_devs[i].num_resources = 1;
  563. regulator_devs[i].resources = &regulator_resources[j];
  564. ret = mfd_add_devices(chip->dev, 0, &regulator_devs[i], 1,
  565. &regulator_resources[j], 0);
  566. if (ret < 0) {
  567. dev_err(chip->dev, "Failed to add regulator subdev\n");
  568. goto out;
  569. }
  570. }
  571. out:
  572. return;
  573. }
  574. static void __devinit device_touch_init(struct pm860x_chip *chip,
  575. struct i2c_client *i2c,
  576. struct pm860x_platform_data *pdata)
  577. {
  578. int ret;
  579. if ((pdata == NULL) || (pdata->touch == NULL))
  580. return;
  581. memcpy(&touch_pdata, pdata->touch, sizeof(struct pm860x_touch_pdata));
  582. touch_devs[0].mfd_data = &touch_pdata;
  583. touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
  584. touch_devs[0].resources = &touch_resources[0];
  585. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  586. ARRAY_SIZE(touch_devs), &touch_resources[0],
  587. chip->irq_base);
  588. if (ret < 0)
  589. dev_err(chip->dev, "Failed to add touch subdev\n");
  590. }
  591. static void __devinit device_power_init(struct pm860x_chip *chip,
  592. struct i2c_client *i2c,
  593. struct pm860x_platform_data *pdata)
  594. {
  595. int ret;
  596. if ((pdata == NULL) || (pdata->power == NULL))
  597. return;
  598. memcpy(&power_pdata, pdata->power, sizeof(struct pm860x_power_pdata));
  599. power_devs[0].mfd_data = &power_pdata;
  600. power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
  601. power_devs[0].resources = &battery_resources[0],
  602. ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
  603. &battery_resources[0], chip->irq_base);
  604. if (ret < 0)
  605. dev_err(chip->dev, "Failed to add battery subdev\n");
  606. power_devs[1].mfd_data = &power_pdata;
  607. power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
  608. power_devs[1].resources = &charger_resources[0],
  609. ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
  610. &charger_resources[0], chip->irq_base);
  611. if (ret < 0)
  612. dev_err(chip->dev, "Failed to add charger subdev\n");
  613. }
  614. static void __devinit device_onkey_init(struct pm860x_chip *chip,
  615. struct i2c_client *i2c,
  616. struct pm860x_platform_data *pdata)
  617. {
  618. int ret;
  619. onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
  620. onkey_devs[0].resources = &onkey_resources[0],
  621. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  622. ARRAY_SIZE(onkey_devs), &onkey_resources[0],
  623. chip->irq_base);
  624. if (ret < 0)
  625. dev_err(chip->dev, "Failed to add onkey subdev\n");
  626. }
  627. static void __devinit device_codec_init(struct pm860x_chip *chip,
  628. struct i2c_client *i2c,
  629. struct pm860x_platform_data *pdata)
  630. {
  631. int ret;
  632. codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
  633. codec_devs[0].resources = &codec_resources[0],
  634. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  635. ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
  636. if (ret < 0)
  637. dev_err(chip->dev, "Failed to add codec subdev\n");
  638. }
  639. static void __devinit device_8607_init(struct pm860x_chip *chip,
  640. struct i2c_client *i2c,
  641. struct pm860x_platform_data *pdata)
  642. {
  643. int data, ret;
  644. ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
  645. if (ret < 0) {
  646. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  647. goto out;
  648. }
  649. switch (ret & PM8607_VERSION_MASK) {
  650. case 0x40:
  651. case 0x50:
  652. dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
  653. ret);
  654. break;
  655. default:
  656. dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
  657. "Chip ID: %02x\n", ret);
  658. goto out;
  659. }
  660. ret = pm860x_reg_read(i2c, PM8607_BUCK3);
  661. if (ret < 0) {
  662. dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
  663. goto out;
  664. }
  665. if (ret & PM8607_BUCK3_DOUBLE)
  666. chip->buck3_double = 1;
  667. ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
  668. if (ret < 0) {
  669. dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
  670. goto out;
  671. }
  672. if (pdata && (pdata->i2c_port == PI2C_PORT))
  673. data = PM8607_B0_MISC1_PI2C;
  674. else
  675. data = 0;
  676. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
  677. if (ret < 0) {
  678. dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
  679. goto out;
  680. }
  681. ret = device_gpadc_init(chip, pdata);
  682. if (ret < 0)
  683. goto out;
  684. ret = device_irq_init(chip, pdata);
  685. if (ret < 0)
  686. goto out;
  687. device_regulator_init(chip, i2c, pdata);
  688. device_onkey_init(chip, i2c, pdata);
  689. device_touch_init(chip, i2c, pdata);
  690. device_power_init(chip, i2c, pdata);
  691. device_codec_init(chip, i2c, pdata);
  692. out:
  693. return;
  694. }
  695. int __devinit pm860x_device_init(struct pm860x_chip *chip,
  696. struct pm860x_platform_data *pdata)
  697. {
  698. chip->core_irq = 0;
  699. switch (chip->id) {
  700. case CHIP_PM8606:
  701. device_bk_init(chip, chip->client, pdata);
  702. device_led_init(chip, chip->client, pdata);
  703. break;
  704. case CHIP_PM8607:
  705. device_8607_init(chip, chip->client, pdata);
  706. break;
  707. }
  708. if (chip->companion) {
  709. switch (chip->id) {
  710. case CHIP_PM8607:
  711. device_bk_init(chip, chip->companion, pdata);
  712. device_led_init(chip, chip->companion, pdata);
  713. break;
  714. case CHIP_PM8606:
  715. device_8607_init(chip, chip->companion, pdata);
  716. break;
  717. }
  718. }
  719. return 0;
  720. }
  721. void __devexit pm860x_device_exit(struct pm860x_chip *chip)
  722. {
  723. device_irq_exit(chip);
  724. mfd_remove_devices(chip->dev);
  725. }
  726. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
  727. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  728. MODULE_LICENSE("GPL");