twl4030-core.c 22 KB

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