twl-core.c 24 KB

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