twl4030-core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
  3. *
  4. * Copyright (C) 2005-2006 Texas Instruments, Inc.
  5. *
  6. * Modifications to defer interrupt handling to a kernel thread:
  7. * Copyright (C) 2006 MontaVista Software, Inc.
  8. *
  9. * Based on tlv320aic23.c:
  10. * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
  11. *
  12. * Code cleanup and modifications to IRQ handler.
  13. * by syed khasim <x0khasim@ti.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/init.h>
  30. #include <linux/mutex.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/clk.h>
  33. #include <linux/err.h>
  34. #include <linux/regulator/machine.h>
  35. #include <linux/i2c.h>
  36. #include <linux/i2c/twl4030.h>
  37. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  38. #include <mach/cpu.h>
  39. #endif
  40. /*
  41. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  42. * Management and System Companion Device" chips originally designed for
  43. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  44. * often at around 3 Mbit/sec, including for interrupt handling.
  45. *
  46. * This driver core provides genirq support for the interrupts emitted,
  47. * by the various modules, and exports register access primitives.
  48. *
  49. * FIXME this driver currently requires use of the first interrupt line
  50. * (and associated registers).
  51. */
  52. #define DRIVER_NAME "twl4030"
  53. #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
  54. defined(CONFIG_TWL4030_BCI_BATTERY_MODULE)
  55. #define twl_has_bci() true
  56. #else
  57. #define twl_has_bci() false
  58. #endif
  59. #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
  60. #define twl_has_keypad() true
  61. #else
  62. #define twl_has_keypad() false
  63. #endif
  64. #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
  65. #define twl_has_gpio() true
  66. #else
  67. #define twl_has_gpio() false
  68. #endif
  69. #if defined(CONFIG_REGULATOR_TWL4030) \
  70. || defined(CONFIG_REGULATOR_TWL4030_MODULE)
  71. #define twl_has_regulator() true
  72. #else
  73. #define twl_has_regulator() false
  74. #endif
  75. #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
  76. #define twl_has_madc() true
  77. #else
  78. #define twl_has_madc() false
  79. #endif
  80. #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
  81. #define twl_has_rtc() true
  82. #else
  83. #define twl_has_rtc() false
  84. #endif
  85. #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
  86. #define twl_has_usb() true
  87. #else
  88. #define twl_has_usb() false
  89. #endif
  90. /* Triton Core internal information (BEGIN) */
  91. /* Last - for index max*/
  92. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  93. #define TWL4030_NUM_SLAVES 4
  94. /* Base Address defns for twl4030_map[] */
  95. /* subchip/slave 0 - USB ID */
  96. #define TWL4030_BASEADD_USB 0x0000
  97. /* subchip/slave 1 - AUD ID */
  98. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  99. #define TWL4030_BASEADD_GPIO 0x0098
  100. #define TWL4030_BASEADD_INTBR 0x0085
  101. #define TWL4030_BASEADD_PIH 0x0080
  102. #define TWL4030_BASEADD_TEST 0x004C
  103. /* subchip/slave 2 - AUX ID */
  104. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  105. #define TWL4030_BASEADD_LED 0x00EE
  106. #define TWL4030_BASEADD_MADC 0x0000
  107. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  108. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  109. #define TWL4030_BASEADD_PWM0 0x00F8
  110. #define TWL4030_BASEADD_PWM1 0x00FB
  111. #define TWL4030_BASEADD_PWMA 0x00EF
  112. #define TWL4030_BASEADD_PWMB 0x00F1
  113. #define TWL4030_BASEADD_KEYPAD 0x00D2
  114. /* subchip/slave 3 - POWER ID */
  115. #define TWL4030_BASEADD_BACKUP 0x0014
  116. #define TWL4030_BASEADD_INT 0x002E
  117. #define TWL4030_BASEADD_PM_MASTER 0x0036
  118. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  119. #define TWL4030_BASEADD_RTC 0x001C
  120. #define TWL4030_BASEADD_SECURED_REG 0x0000
  121. /* Triton Core internal information (END) */
  122. /* Few power values */
  123. #define R_CFG_BOOT 0x05
  124. #define R_PROTECT_KEY 0x0E
  125. /* access control values for R_PROTECT_KEY */
  126. #define KEY_UNLOCK1 0xce
  127. #define KEY_UNLOCK2 0xec
  128. #define KEY_LOCK 0x00
  129. /* some fields in R_CFG_BOOT */
  130. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  131. #define HFCLK_FREQ_26_MHZ (2 << 0)
  132. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  133. #define HIGH_PERF_SQ (1 << 3)
  134. /* chip-specific feature flags, for i2c_device_id.driver_data */
  135. #define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
  136. #define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
  137. /*----------------------------------------------------------------------*/
  138. /* is driver active, bound to a chip? */
  139. static bool inuse;
  140. /* Structure for each TWL4030 Slave */
  141. struct twl4030_client {
  142. struct i2c_client *client;
  143. u8 address;
  144. /* max numb of i2c_msg required is for read =2 */
  145. struct i2c_msg xfer_msg[2];
  146. /* To lock access to xfer_msg */
  147. struct mutex xfer_lock;
  148. };
  149. static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
  150. /* mapping the module id to slave id and base address */
  151. struct twl4030mapping {
  152. unsigned char sid; /* Slave ID */
  153. unsigned char base; /* base address */
  154. };
  155. static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  156. /*
  157. * NOTE: don't change this table without updating the
  158. * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
  159. * so they continue to match the order in this table.
  160. */
  161. { 0, TWL4030_BASEADD_USB },
  162. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  163. { 1, TWL4030_BASEADD_GPIO },
  164. { 1, TWL4030_BASEADD_INTBR },
  165. { 1, TWL4030_BASEADD_PIH },
  166. { 1, TWL4030_BASEADD_TEST },
  167. { 2, TWL4030_BASEADD_KEYPAD },
  168. { 2, TWL4030_BASEADD_MADC },
  169. { 2, TWL4030_BASEADD_INTERRUPTS },
  170. { 2, TWL4030_BASEADD_LED },
  171. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  172. { 2, TWL4030_BASEADD_PRECHARGE },
  173. { 2, TWL4030_BASEADD_PWM0 },
  174. { 2, TWL4030_BASEADD_PWM1 },
  175. { 2, TWL4030_BASEADD_PWMA },
  176. { 2, TWL4030_BASEADD_PWMB },
  177. { 3, TWL4030_BASEADD_BACKUP },
  178. { 3, TWL4030_BASEADD_INT },
  179. { 3, TWL4030_BASEADD_PM_MASTER },
  180. { 3, TWL4030_BASEADD_PM_RECEIVER },
  181. { 3, TWL4030_BASEADD_RTC },
  182. { 3, TWL4030_BASEADD_SECURED_REG },
  183. };
  184. /*----------------------------------------------------------------------*/
  185. /* Exported Functions */
  186. /**
  187. * twl4030_i2c_write - Writes a n bit register in TWL4030
  188. * @mod_no: module number
  189. * @value: an array of num_bytes+1 containing data to write
  190. * @reg: register address (just offset will do)
  191. * @num_bytes: number of bytes to transfer
  192. *
  193. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  194. * valid data starts at Offset 1.
  195. *
  196. * Returns the result of operation - 0 is success
  197. */
  198. int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  199. {
  200. int ret;
  201. int sid;
  202. struct twl4030_client *twl;
  203. struct i2c_msg *msg;
  204. if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
  205. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  206. return -EPERM;
  207. }
  208. sid = twl4030_map[mod_no].sid;
  209. twl = &twl4030_modules[sid];
  210. if (unlikely(!inuse)) {
  211. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  212. return -EPERM;
  213. }
  214. mutex_lock(&twl->xfer_lock);
  215. /*
  216. * [MSG1]: fill the register address data
  217. * fill the data Tx buffer
  218. */
  219. msg = &twl->xfer_msg[0];
  220. msg->addr = twl->address;
  221. msg->len = num_bytes + 1;
  222. msg->flags = 0;
  223. msg->buf = value;
  224. /* over write the first byte of buffer with the register address */
  225. *value = twl4030_map[mod_no].base + reg;
  226. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  227. mutex_unlock(&twl->xfer_lock);
  228. /* i2cTransfer returns num messages.translate it pls.. */
  229. if (ret >= 0)
  230. ret = 0;
  231. return ret;
  232. }
  233. EXPORT_SYMBOL(twl4030_i2c_write);
  234. /**
  235. * twl4030_i2c_read - Reads a n bit register in TWL4030
  236. * @mod_no: module number
  237. * @value: an array of num_bytes containing data to be read
  238. * @reg: register address (just offset will do)
  239. * @num_bytes: number of bytes to transfer
  240. *
  241. * Returns result of operation - num_bytes is success else failure.
  242. */
  243. int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  244. {
  245. int ret;
  246. u8 val;
  247. int sid;
  248. struct twl4030_client *twl;
  249. struct i2c_msg *msg;
  250. if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
  251. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  252. return -EPERM;
  253. }
  254. sid = twl4030_map[mod_no].sid;
  255. twl = &twl4030_modules[sid];
  256. if (unlikely(!inuse)) {
  257. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  258. return -EPERM;
  259. }
  260. mutex_lock(&twl->xfer_lock);
  261. /* [MSG1] fill the register address data */
  262. msg = &twl->xfer_msg[0];
  263. msg->addr = twl->address;
  264. msg->len = 1;
  265. msg->flags = 0; /* Read the register value */
  266. val = twl4030_map[mod_no].base + reg;
  267. msg->buf = &val;
  268. /* [MSG2] fill the data rx buffer */
  269. msg = &twl->xfer_msg[1];
  270. msg->addr = twl->address;
  271. msg->flags = I2C_M_RD; /* Read the register value */
  272. msg->len = num_bytes; /* only n bytes */
  273. msg->buf = value;
  274. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  275. mutex_unlock(&twl->xfer_lock);
  276. /* i2cTransfer returns num messages.translate it pls.. */
  277. if (ret >= 0)
  278. ret = 0;
  279. return ret;
  280. }
  281. EXPORT_SYMBOL(twl4030_i2c_read);
  282. /**
  283. * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
  284. * @mod_no: module number
  285. * @value: the value to be written 8 bit
  286. * @reg: register address (just offset will do)
  287. *
  288. * Returns result of operation - 0 is success
  289. */
  290. int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  291. {
  292. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  293. u8 temp_buffer[2] = { 0 };
  294. /* offset 1 contains the data */
  295. temp_buffer[1] = value;
  296. return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
  297. }
  298. EXPORT_SYMBOL(twl4030_i2c_write_u8);
  299. /**
  300. * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
  301. * @mod_no: module number
  302. * @value: the value read 8 bit
  303. * @reg: register address (just offset will do)
  304. *
  305. * Returns result of operation - 0 is success
  306. */
  307. int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  308. {
  309. return twl4030_i2c_read(mod_no, value, reg, 1);
  310. }
  311. EXPORT_SYMBOL(twl4030_i2c_read_u8);
  312. /*----------------------------------------------------------------------*/
  313. static struct device *
  314. add_numbered_child(unsigned chip, const char *name, int num,
  315. void *pdata, unsigned pdata_len,
  316. bool can_wakeup, int irq0, int irq1)
  317. {
  318. struct platform_device *pdev;
  319. struct twl4030_client *twl = &twl4030_modules[chip];
  320. int status;
  321. pdev = platform_device_alloc(name, num);
  322. if (!pdev) {
  323. dev_dbg(&twl->client->dev, "can't alloc dev\n");
  324. status = -ENOMEM;
  325. goto err;
  326. }
  327. device_init_wakeup(&pdev->dev, can_wakeup);
  328. pdev->dev.parent = &twl->client->dev;
  329. if (pdata) {
  330. status = platform_device_add_data(pdev, pdata, pdata_len);
  331. if (status < 0) {
  332. dev_dbg(&pdev->dev, "can't add platform_data\n");
  333. goto err;
  334. }
  335. }
  336. if (irq0) {
  337. struct resource r[2] = {
  338. { .start = irq0, .flags = IORESOURCE_IRQ, },
  339. { .start = irq1, .flags = IORESOURCE_IRQ, },
  340. };
  341. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  342. if (status < 0) {
  343. dev_dbg(&pdev->dev, "can't add irqs\n");
  344. goto err;
  345. }
  346. }
  347. status = platform_device_add(pdev);
  348. err:
  349. if (status < 0) {
  350. platform_device_put(pdev);
  351. dev_err(&twl->client->dev, "can't add %s dev\n", name);
  352. return ERR_PTR(status);
  353. }
  354. return &pdev->dev;
  355. }
  356. static inline struct device *add_child(unsigned chip, const char *name,
  357. void *pdata, unsigned pdata_len,
  358. bool can_wakeup, int irq0, int irq1)
  359. {
  360. return add_numbered_child(chip, name, -1, pdata, pdata_len,
  361. can_wakeup, irq0, irq1);
  362. }
  363. static struct device *
  364. add_regulator_linked(int num, struct regulator_init_data *pdata,
  365. struct regulator_consumer_supply *consumers,
  366. unsigned num_consumers)
  367. {
  368. /* regulator framework demands init_data ... */
  369. if (!pdata)
  370. return NULL;
  371. if (consumers) {
  372. pdata->consumer_supplies = consumers;
  373. pdata->num_consumer_supplies = num_consumers;
  374. }
  375. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  376. return add_numbered_child(3, "twl4030_reg", num,
  377. pdata, sizeof(*pdata), false, 0, 0);
  378. }
  379. static struct device *
  380. add_regulator(int num, struct regulator_init_data *pdata)
  381. {
  382. return add_regulator_linked(num, pdata, NULL, 0);
  383. }
  384. /*
  385. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  386. * for the PIH, and the next are for the PWR_INT SIH, since
  387. * that's how twl_init_irq() sets things up.
  388. */
  389. static int
  390. add_children(struct twl4030_platform_data *pdata, unsigned long features)
  391. {
  392. struct device *child;
  393. struct device *usb_transceiver = NULL;
  394. if (twl_has_bci() && pdata->bci && !(features & TPS_SUBSET)) {
  395. child = add_child(3, "twl4030_bci",
  396. pdata->bci, sizeof(*pdata->bci),
  397. false,
  398. /* irq0 = CHG_PRES, irq1 = BCI */
  399. pdata->irq_base + 8 + 1, pdata->irq_base + 2);
  400. if (IS_ERR(child))
  401. return PTR_ERR(child);
  402. }
  403. if (twl_has_gpio() && pdata->gpio) {
  404. child = add_child(1, "twl4030_gpio",
  405. pdata->gpio, sizeof(*pdata->gpio),
  406. false, pdata->irq_base + 0, 0);
  407. if (IS_ERR(child))
  408. return PTR_ERR(child);
  409. }
  410. if (twl_has_keypad() && pdata->keypad) {
  411. child = add_child(2, "twl4030_keypad",
  412. pdata->keypad, sizeof(*pdata->keypad),
  413. true, pdata->irq_base + 1, 0);
  414. if (IS_ERR(child))
  415. return PTR_ERR(child);
  416. }
  417. if (twl_has_madc() && pdata->madc) {
  418. child = add_child(2, "twl4030_madc",
  419. pdata->madc, sizeof(*pdata->madc),
  420. true, pdata->irq_base + 3, 0);
  421. if (IS_ERR(child))
  422. return PTR_ERR(child);
  423. }
  424. if (twl_has_rtc()) {
  425. /*
  426. * REVISIT platform_data here currently might expose the
  427. * "msecure" line ... but for now we just expect board
  428. * setup to tell the chip "it's always ok to SET_TIME".
  429. * Eventually, Linux might become more aware of such
  430. * HW security concerns, and "least privilege".
  431. */
  432. child = add_child(3, "twl4030_rtc",
  433. NULL, 0,
  434. true, pdata->irq_base + 8 + 3, 0);
  435. if (IS_ERR(child))
  436. return PTR_ERR(child);
  437. }
  438. if (twl_has_usb() && pdata->usb) {
  439. child = add_child(0, "twl4030_usb",
  440. pdata->usb, sizeof(*pdata->usb),
  441. true,
  442. /* irq0 = USB_PRES, irq1 = USB */
  443. pdata->irq_base + 8 + 2, pdata->irq_base + 4);
  444. if (IS_ERR(child))
  445. return PTR_ERR(child);
  446. /* we need to connect regulators to this transceiver */
  447. usb_transceiver = child;
  448. }
  449. if (twl_has_regulator()) {
  450. /*
  451. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
  452. if (IS_ERR(child))
  453. return PTR_ERR(child);
  454. */
  455. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
  456. if (IS_ERR(child))
  457. return PTR_ERR(child);
  458. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
  459. if (IS_ERR(child))
  460. return PTR_ERR(child);
  461. child = add_regulator((features & TWL4030_VAUX2)
  462. ? TWL4030_REG_VAUX2_4030
  463. : TWL4030_REG_VAUX2,
  464. pdata->vaux2);
  465. if (IS_ERR(child))
  466. return PTR_ERR(child);
  467. }
  468. if (twl_has_regulator() && usb_transceiver) {
  469. static struct regulator_consumer_supply usb1v5 = {
  470. .supply = "usb1v5",
  471. };
  472. static struct regulator_consumer_supply usb1v8 = {
  473. .supply = "usb1v8",
  474. };
  475. static struct regulator_consumer_supply usb3v1 = {
  476. .supply = "usb3v1",
  477. };
  478. /* this is a template that gets copied */
  479. struct regulator_init_data usb_fixed = {
  480. .constraints.valid_modes_mask =
  481. REGULATOR_MODE_NORMAL
  482. | REGULATOR_MODE_STANDBY,
  483. .constraints.valid_ops_mask =
  484. REGULATOR_CHANGE_MODE
  485. | REGULATOR_CHANGE_STATUS,
  486. };
  487. usb1v5.dev = usb_transceiver;
  488. usb1v8.dev = usb_transceiver;
  489. usb3v1.dev = usb_transceiver;
  490. child = add_regulator_linked(TWL4030_REG_VUSB1V5, &usb_fixed,
  491. &usb1v5, 1);
  492. if (IS_ERR(child))
  493. return PTR_ERR(child);
  494. child = add_regulator_linked(TWL4030_REG_VUSB1V8, &usb_fixed,
  495. &usb1v8, 1);
  496. if (IS_ERR(child))
  497. return PTR_ERR(child);
  498. child = add_regulator_linked(TWL4030_REG_VUSB3V1, &usb_fixed,
  499. &usb3v1, 1);
  500. if (IS_ERR(child))
  501. return PTR_ERR(child);
  502. }
  503. /* maybe add LDOs that are omitted on cost-reduced parts */
  504. if (twl_has_regulator() && !(features & TPS_SUBSET)) {
  505. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
  506. if (IS_ERR(child))
  507. return PTR_ERR(child);
  508. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
  509. if (IS_ERR(child))
  510. return PTR_ERR(child);
  511. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
  512. if (IS_ERR(child))
  513. return PTR_ERR(child);
  514. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
  515. if (IS_ERR(child))
  516. return PTR_ERR(child);
  517. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
  518. if (IS_ERR(child))
  519. return PTR_ERR(child);
  520. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
  521. if (IS_ERR(child))
  522. return PTR_ERR(child);
  523. }
  524. return 0;
  525. }
  526. /*----------------------------------------------------------------------*/
  527. /*
  528. * These three functions initialize the on-chip clock framework,
  529. * letting it generate the right frequencies for USB, MADC, and
  530. * other purposes.
  531. */
  532. static inline int __init protect_pm_master(void)
  533. {
  534. int e = 0;
  535. e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
  536. R_PROTECT_KEY);
  537. return e;
  538. }
  539. static inline int __init unprotect_pm_master(void)
  540. {
  541. int e = 0;
  542. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
  543. R_PROTECT_KEY);
  544. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
  545. R_PROTECT_KEY);
  546. return e;
  547. }
  548. static void __init clocks_init(struct device *dev)
  549. {
  550. int e = 0;
  551. struct clk *osc;
  552. u32 rate;
  553. u8 ctrl = HFCLK_FREQ_26_MHZ;
  554. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  555. if (cpu_is_omap2430())
  556. osc = clk_get(dev, "osc_ck");
  557. else
  558. osc = clk_get(dev, "osc_sys_ck");
  559. if (IS_ERR(osc)) {
  560. printk(KERN_WARNING "Skipping twl4030 internal clock init and "
  561. "using bootloader value (unknown osc rate)\n");
  562. return;
  563. }
  564. rate = clk_get_rate(osc);
  565. clk_put(osc);
  566. #else
  567. /* REVISIT for non-OMAP systems, pass the clock rate from
  568. * board init code, using platform_data.
  569. */
  570. osc = ERR_PTR(-EIO);
  571. printk(KERN_WARNING "Skipping twl4030 internal clock init and "
  572. "using bootloader value (unknown osc rate)\n");
  573. return;
  574. #endif
  575. switch (rate) {
  576. case 19200000:
  577. ctrl = HFCLK_FREQ_19p2_MHZ;
  578. break;
  579. case 26000000:
  580. ctrl = HFCLK_FREQ_26_MHZ;
  581. break;
  582. case 38400000:
  583. ctrl = HFCLK_FREQ_38p4_MHZ;
  584. break;
  585. }
  586. ctrl |= HIGH_PERF_SQ;
  587. e |= unprotect_pm_master();
  588. /* effect->MADC+USB ck en */
  589. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  590. e |= protect_pm_master();
  591. if (e < 0)
  592. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  593. }
  594. /*----------------------------------------------------------------------*/
  595. int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  596. int twl_exit_irq(void);
  597. static int twl4030_remove(struct i2c_client *client)
  598. {
  599. unsigned i;
  600. int status;
  601. status = twl_exit_irq();
  602. if (status < 0)
  603. return status;
  604. for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
  605. struct twl4030_client *twl = &twl4030_modules[i];
  606. if (twl->client && twl->client != client)
  607. i2c_unregister_device(twl->client);
  608. twl4030_modules[i].client = NULL;
  609. }
  610. inuse = false;
  611. return 0;
  612. }
  613. /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
  614. static int
  615. twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
  616. {
  617. int status;
  618. unsigned i;
  619. struct twl4030_platform_data *pdata = client->dev.platform_data;
  620. if (!pdata) {
  621. dev_dbg(&client->dev, "no platform data?\n");
  622. return -EINVAL;
  623. }
  624. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  625. dev_dbg(&client->dev, "can't talk I2C?\n");
  626. return -EIO;
  627. }
  628. if (inuse) {
  629. dev_dbg(&client->dev, "driver is already in use\n");
  630. return -EBUSY;
  631. }
  632. for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
  633. struct twl4030_client *twl = &twl4030_modules[i];
  634. twl->address = client->addr + i;
  635. if (i == 0)
  636. twl->client = client;
  637. else {
  638. twl->client = i2c_new_dummy(client->adapter,
  639. twl->address);
  640. if (!twl->client) {
  641. dev_err(&twl->client->dev,
  642. "can't attach client %d\n", i);
  643. status = -ENOMEM;
  644. goto fail;
  645. }
  646. strlcpy(twl->client->name, id->name,
  647. sizeof(twl->client->name));
  648. }
  649. mutex_init(&twl->xfer_lock);
  650. }
  651. inuse = true;
  652. /* setup clock framework */
  653. clocks_init(&client->dev);
  654. /* Maybe init the T2 Interrupt subsystem */
  655. if (client->irq
  656. && pdata->irq_base
  657. && pdata->irq_end > pdata->irq_base) {
  658. status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
  659. if (status < 0)
  660. goto fail;
  661. }
  662. status = add_children(pdata, id->driver_data);
  663. fail:
  664. if (status < 0)
  665. twl4030_remove(client);
  666. return status;
  667. }
  668. static const struct i2c_device_id twl4030_ids[] = {
  669. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  670. { "twl5030", 0 }, /* T2 updated */
  671. { "tps65950", 0 }, /* catalog version of twl5030 */
  672. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  673. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  674. { /* end of list */ },
  675. };
  676. MODULE_DEVICE_TABLE(i2c, twl4030_ids);
  677. /* One Client Driver , 4 Clients */
  678. static struct i2c_driver twl4030_driver = {
  679. .driver.name = DRIVER_NAME,
  680. .id_table = twl4030_ids,
  681. .probe = twl4030_probe,
  682. .remove = twl4030_remove,
  683. };
  684. static int __init twl4030_init(void)
  685. {
  686. return i2c_add_driver(&twl4030_driver);
  687. }
  688. subsys_initcall(twl4030_init);
  689. static void __exit twl4030_exit(void)
  690. {
  691. i2c_del_driver(&twl4030_driver);
  692. }
  693. module_exit(twl4030_exit);
  694. MODULE_AUTHOR("Texas Instruments, Inc.");
  695. MODULE_DESCRIPTION("I2C Core interface for TWL4030");
  696. MODULE_LICENSE("GPL");