twl4030-core.c 22 KB

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