twl4030-core.c 21 KB

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