twl4030-core.c 21 KB

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