twl-core.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
  55. #define twl_has_keypad() true
  56. #else
  57. #define twl_has_keypad() false
  58. #endif
  59. #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
  60. #define twl_has_gpio() true
  61. #else
  62. #define twl_has_gpio() false
  63. #endif
  64. #if defined(CONFIG_REGULATOR_TWL4030) \
  65. || defined(CONFIG_REGULATOR_TWL4030_MODULE)
  66. #define twl_has_regulator() true
  67. #else
  68. #define twl_has_regulator() false
  69. #endif
  70. #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
  71. #define twl_has_madc() true
  72. #else
  73. #define twl_has_madc() false
  74. #endif
  75. #ifdef CONFIG_TWL4030_POWER
  76. #define twl_has_power() true
  77. #else
  78. #define twl_has_power() false
  79. #endif
  80. #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
  81. #define twl_has_rtc() true
  82. #else
  83. #define twl_has_rtc() false
  84. #endif
  85. #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
  86. #define twl_has_usb() true
  87. #else
  88. #define twl_has_usb() false
  89. #endif
  90. #if defined(CONFIG_TWL4030_WATCHDOG) || \
  91. defined(CONFIG_TWL4030_WATCHDOG_MODULE)
  92. #define twl_has_watchdog() true
  93. #else
  94. #define twl_has_watchdog() false
  95. #endif
  96. #if defined(CONFIG_TWL4030_CODEC) || defined(CONFIG_TWL4030_CODEC_MODULE) ||\
  97. defined(CONFIG_SND_SOC_TWL6030) || defined(CONFIG_SND_SOC_TWL6030_MODULE)
  98. #define twl_has_codec() true
  99. #else
  100. #define twl_has_codec() false
  101. #endif
  102. /* Triton Core internal information (BEGIN) */
  103. /* Last - for index max*/
  104. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  105. #define TWL_NUM_SLAVES 4
  106. #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
  107. || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
  108. #define twl_has_pwrbutton() true
  109. #else
  110. #define twl_has_pwrbutton() false
  111. #endif
  112. #define SUB_CHIP_ID0 0
  113. #define SUB_CHIP_ID1 1
  114. #define SUB_CHIP_ID2 2
  115. #define SUB_CHIP_ID3 3
  116. #define TWL_MODULE_LAST TWL4030_MODULE_LAST
  117. /* Base Address defns for twl4030_map[] */
  118. /* subchip/slave 0 - USB ID */
  119. #define TWL4030_BASEADD_USB 0x0000
  120. /* subchip/slave 1 - AUD ID */
  121. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  122. #define TWL4030_BASEADD_GPIO 0x0098
  123. #define TWL4030_BASEADD_INTBR 0x0085
  124. #define TWL4030_BASEADD_PIH 0x0080
  125. #define TWL4030_BASEADD_TEST 0x004C
  126. /* subchip/slave 2 - AUX ID */
  127. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  128. #define TWL4030_BASEADD_LED 0x00EE
  129. #define TWL4030_BASEADD_MADC 0x0000
  130. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  131. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  132. #define TWL4030_BASEADD_PWM0 0x00F8
  133. #define TWL4030_BASEADD_PWM1 0x00FB
  134. #define TWL4030_BASEADD_PWMA 0x00EF
  135. #define TWL4030_BASEADD_PWMB 0x00F1
  136. #define TWL4030_BASEADD_KEYPAD 0x00D2
  137. #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
  138. #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
  139. one */
  140. /* subchip/slave 3 - POWER ID */
  141. #define TWL4030_BASEADD_BACKUP 0x0014
  142. #define TWL4030_BASEADD_INT 0x002E
  143. #define TWL4030_BASEADD_PM_MASTER 0x0036
  144. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  145. #define TWL4030_BASEADD_RTC 0x001C
  146. #define TWL4030_BASEADD_SECURED_REG 0x0000
  147. /* Triton Core internal information (END) */
  148. /* subchip/slave 0 0x48 - POWER */
  149. #define TWL6030_BASEADD_RTC 0x0000
  150. #define TWL6030_BASEADD_MEM 0x0017
  151. #define TWL6030_BASEADD_PM_MASTER 0x001F
  152. #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
  153. #define TWL6030_BASEADD_PM_MISC 0x00E2
  154. #define TWL6030_BASEADD_PM_PUPD 0x00F0
  155. /* subchip/slave 1 0x49 - FEATURE */
  156. #define TWL6030_BASEADD_USB 0x0000
  157. #define TWL6030_BASEADD_GPADC_CTRL 0x002E
  158. #define TWL6030_BASEADD_AUX 0x0090
  159. #define TWL6030_BASEADD_PWM 0x00BA
  160. #define TWL6030_BASEADD_GASGAUGE 0x00C0
  161. #define TWL6030_BASEADD_PIH 0x00D0
  162. #define TWL6030_BASEADD_CHARGER 0x00E0
  163. /* subchip/slave 2 0x4A - DFT */
  164. #define TWL6030_BASEADD_DIEID 0x00C0
  165. /* subchip/slave 3 0x4B - AUDIO */
  166. #define TWL6030_BASEADD_AUDIO 0x0000
  167. #define TWL6030_BASEADD_RSV 0x0000
  168. /* Few power values */
  169. #define R_CFG_BOOT 0x05
  170. #define R_PROTECT_KEY 0x0E
  171. /* access control values for R_PROTECT_KEY */
  172. #define KEY_UNLOCK1 0xce
  173. #define KEY_UNLOCK2 0xec
  174. #define KEY_LOCK 0x00
  175. /* some fields in R_CFG_BOOT */
  176. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  177. #define HFCLK_FREQ_26_MHZ (2 << 0)
  178. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  179. #define HIGH_PERF_SQ (1 << 3)
  180. #define CK32K_LOWPWR_EN (1 << 7)
  181. /* chip-specific feature flags, for i2c_device_id.driver_data */
  182. #define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
  183. #define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
  184. #define TWL5031 BIT(2) /* twl5031 has different registers */
  185. #define TWL6030_CLASS BIT(3) /* TWL6030 class */
  186. /*----------------------------------------------------------------------*/
  187. /* is driver active, bound to a chip? */
  188. static bool inuse;
  189. static unsigned int twl_id;
  190. unsigned int twl_rev(void)
  191. {
  192. return twl_id;
  193. }
  194. EXPORT_SYMBOL(twl_rev);
  195. /* Structure for each TWL4030/TWL6030 Slave */
  196. struct twl_client {
  197. struct i2c_client *client;
  198. u8 address;
  199. /* max numb of i2c_msg required is for read =2 */
  200. struct i2c_msg xfer_msg[2];
  201. /* To lock access to xfer_msg */
  202. struct mutex xfer_lock;
  203. };
  204. static struct twl_client twl_modules[TWL_NUM_SLAVES];
  205. /* mapping the module id to slave id and base address */
  206. struct twl_mapping {
  207. unsigned char sid; /* Slave ID */
  208. unsigned char base; /* base address */
  209. };
  210. struct twl_mapping *twl_map;
  211. static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  212. /*
  213. * NOTE: don't change this table without updating the
  214. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  215. * so they continue to match the order in this table.
  216. */
  217. { 0, TWL4030_BASEADD_USB },
  218. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  219. { 1, TWL4030_BASEADD_GPIO },
  220. { 1, TWL4030_BASEADD_INTBR },
  221. { 1, TWL4030_BASEADD_PIH },
  222. { 1, TWL4030_BASEADD_TEST },
  223. { 2, TWL4030_BASEADD_KEYPAD },
  224. { 2, TWL4030_BASEADD_MADC },
  225. { 2, TWL4030_BASEADD_INTERRUPTS },
  226. { 2, TWL4030_BASEADD_LED },
  227. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  228. { 2, TWL4030_BASEADD_PRECHARGE },
  229. { 2, TWL4030_BASEADD_PWM0 },
  230. { 2, TWL4030_BASEADD_PWM1 },
  231. { 2, TWL4030_BASEADD_PWMA },
  232. { 2, TWL4030_BASEADD_PWMB },
  233. { 2, TWL5031_BASEADD_ACCESSORY },
  234. { 2, TWL5031_BASEADD_INTERRUPTS },
  235. { 3, TWL4030_BASEADD_BACKUP },
  236. { 3, TWL4030_BASEADD_INT },
  237. { 3, TWL4030_BASEADD_PM_MASTER },
  238. { 3, TWL4030_BASEADD_PM_RECEIVER },
  239. { 3, TWL4030_BASEADD_RTC },
  240. { 3, TWL4030_BASEADD_SECURED_REG },
  241. };
  242. static struct twl_mapping twl6030_map[] = {
  243. /*
  244. * NOTE: don't change this table without updating the
  245. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  246. * so they continue to match the order in this table.
  247. */
  248. { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
  249. { SUB_CHIP_ID3, TWL6030_BASEADD_AUDIO },
  250. { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
  251. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  252. { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
  253. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  254. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  255. { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
  256. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  257. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  258. { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
  259. { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
  260. { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
  261. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  262. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  263. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  264. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  265. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  266. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
  267. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
  268. { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
  269. { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
  270. };
  271. /*----------------------------------------------------------------------*/
  272. /* Exported Functions */
  273. /**
  274. * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
  275. * @mod_no: module number
  276. * @value: an array of num_bytes+1 containing data to write
  277. * @reg: register address (just offset will do)
  278. * @num_bytes: number of bytes to transfer
  279. *
  280. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  281. * valid data starts at Offset 1.
  282. *
  283. * Returns the result of operation - 0 is success
  284. */
  285. int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  286. {
  287. int ret;
  288. int sid;
  289. struct twl_client *twl;
  290. struct i2c_msg *msg;
  291. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  292. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  293. return -EPERM;
  294. }
  295. sid = twl_map[mod_no].sid;
  296. twl = &twl_modules[sid];
  297. if (unlikely(!inuse)) {
  298. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  299. return -EPERM;
  300. }
  301. mutex_lock(&twl->xfer_lock);
  302. /*
  303. * [MSG1]: fill the register address data
  304. * fill the data Tx buffer
  305. */
  306. msg = &twl->xfer_msg[0];
  307. msg->addr = twl->address;
  308. msg->len = num_bytes + 1;
  309. msg->flags = 0;
  310. msg->buf = value;
  311. /* over write the first byte of buffer with the register address */
  312. *value = twl_map[mod_no].base + reg;
  313. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  314. mutex_unlock(&twl->xfer_lock);
  315. /* i2c_transfer returns number of messages transferred */
  316. if (ret != 1) {
  317. pr_err("%s: i2c_write failed to transfer all messages\n",
  318. DRIVER_NAME);
  319. if (ret < 0)
  320. return ret;
  321. else
  322. return -EIO;
  323. } else {
  324. return 0;
  325. }
  326. }
  327. EXPORT_SYMBOL(twl_i2c_write);
  328. /**
  329. * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
  330. * @mod_no: module number
  331. * @value: an array of num_bytes containing data to be read
  332. * @reg: register address (just offset will do)
  333. * @num_bytes: number of bytes to transfer
  334. *
  335. * Returns result of operation - num_bytes is success else failure.
  336. */
  337. int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  338. {
  339. int ret;
  340. u8 val;
  341. int sid;
  342. struct twl_client *twl;
  343. struct i2c_msg *msg;
  344. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  345. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  346. return -EPERM;
  347. }
  348. sid = twl_map[mod_no].sid;
  349. twl = &twl_modules[sid];
  350. if (unlikely(!inuse)) {
  351. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  352. return -EPERM;
  353. }
  354. mutex_lock(&twl->xfer_lock);
  355. /* [MSG1] fill the register address data */
  356. msg = &twl->xfer_msg[0];
  357. msg->addr = twl->address;
  358. msg->len = 1;
  359. msg->flags = 0; /* Read the register value */
  360. val = twl_map[mod_no].base + reg;
  361. msg->buf = &val;
  362. /* [MSG2] fill the data rx buffer */
  363. msg = &twl->xfer_msg[1];
  364. msg->addr = twl->address;
  365. msg->flags = I2C_M_RD; /* Read the register value */
  366. msg->len = num_bytes; /* only n bytes */
  367. msg->buf = value;
  368. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  369. mutex_unlock(&twl->xfer_lock);
  370. /* i2c_transfer returns number of messages transferred */
  371. if (ret != 2) {
  372. pr_err("%s: i2c_read failed to transfer all messages\n",
  373. DRIVER_NAME);
  374. if (ret < 0)
  375. return ret;
  376. else
  377. return -EIO;
  378. } else {
  379. return 0;
  380. }
  381. }
  382. EXPORT_SYMBOL(twl_i2c_read);
  383. /**
  384. * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
  385. * @mod_no: module number
  386. * @value: the value to be written 8 bit
  387. * @reg: register address (just offset will do)
  388. *
  389. * Returns result of operation - 0 is success
  390. */
  391. int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  392. {
  393. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  394. u8 temp_buffer[2] = { 0 };
  395. /* offset 1 contains the data */
  396. temp_buffer[1] = value;
  397. return twl_i2c_write(mod_no, temp_buffer, reg, 1);
  398. }
  399. EXPORT_SYMBOL(twl_i2c_write_u8);
  400. /**
  401. * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
  402. * @mod_no: module number
  403. * @value: the value read 8 bit
  404. * @reg: register address (just offset will do)
  405. *
  406. * Returns result of operation - 0 is success
  407. */
  408. int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  409. {
  410. return twl_i2c_read(mod_no, value, reg, 1);
  411. }
  412. EXPORT_SYMBOL(twl_i2c_read_u8);
  413. /*----------------------------------------------------------------------*/
  414. static struct device *
  415. add_numbered_child(unsigned chip, const char *name, int num,
  416. void *pdata, unsigned pdata_len,
  417. bool can_wakeup, int irq0, int irq1)
  418. {
  419. struct platform_device *pdev;
  420. struct twl_client *twl = &twl_modules[chip];
  421. int status;
  422. pdev = platform_device_alloc(name, num);
  423. if (!pdev) {
  424. dev_dbg(&twl->client->dev, "can't alloc dev\n");
  425. status = -ENOMEM;
  426. goto err;
  427. }
  428. device_init_wakeup(&pdev->dev, can_wakeup);
  429. pdev->dev.parent = &twl->client->dev;
  430. if (pdata) {
  431. status = platform_device_add_data(pdev, pdata, pdata_len);
  432. if (status < 0) {
  433. dev_dbg(&pdev->dev, "can't add platform_data\n");
  434. goto err;
  435. }
  436. }
  437. if (irq0) {
  438. struct resource r[2] = {
  439. { .start = irq0, .flags = IORESOURCE_IRQ, },
  440. { .start = irq1, .flags = IORESOURCE_IRQ, },
  441. };
  442. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  443. if (status < 0) {
  444. dev_dbg(&pdev->dev, "can't add irqs\n");
  445. goto err;
  446. }
  447. }
  448. status = platform_device_add(pdev);
  449. err:
  450. if (status < 0) {
  451. platform_device_put(pdev);
  452. dev_err(&twl->client->dev, "can't add %s dev\n", name);
  453. return ERR_PTR(status);
  454. }
  455. return &pdev->dev;
  456. }
  457. static inline struct device *add_child(unsigned chip, const char *name,
  458. void *pdata, unsigned pdata_len,
  459. bool can_wakeup, int irq0, int irq1)
  460. {
  461. return add_numbered_child(chip, name, -1, pdata, pdata_len,
  462. can_wakeup, irq0, irq1);
  463. }
  464. static struct device *
  465. add_regulator_linked(int num, struct regulator_init_data *pdata,
  466. struct regulator_consumer_supply *consumers,
  467. unsigned num_consumers)
  468. {
  469. unsigned sub_chip_id;
  470. /* regulator framework demands init_data ... */
  471. if (!pdata)
  472. return NULL;
  473. if (consumers) {
  474. pdata->consumer_supplies = consumers;
  475. pdata->num_consumer_supplies = num_consumers;
  476. }
  477. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  478. sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
  479. return add_numbered_child(sub_chip_id, "twl_reg", num,
  480. pdata, sizeof(*pdata), false, 0, 0);
  481. }
  482. static struct device *
  483. add_regulator(int num, struct regulator_init_data *pdata)
  484. {
  485. return add_regulator_linked(num, pdata, NULL, 0);
  486. }
  487. /*
  488. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  489. * for the PIH, and the next are for the PWR_INT SIH, since
  490. * that's how twl_init_irq() sets things up.
  491. */
  492. static int
  493. add_children(struct twl4030_platform_data *pdata, unsigned long features)
  494. {
  495. struct device *child;
  496. unsigned sub_chip_id;
  497. if (twl_has_gpio() && pdata->gpio) {
  498. child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
  499. pdata->gpio, sizeof(*pdata->gpio),
  500. false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
  501. if (IS_ERR(child))
  502. return PTR_ERR(child);
  503. }
  504. if (twl_has_keypad() && pdata->keypad) {
  505. child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
  506. pdata->keypad, sizeof(*pdata->keypad),
  507. true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
  508. if (IS_ERR(child))
  509. return PTR_ERR(child);
  510. }
  511. if (twl_has_madc() && pdata->madc) {
  512. child = add_child(2, "twl4030_madc",
  513. pdata->madc, sizeof(*pdata->madc),
  514. true, pdata->irq_base + MADC_INTR_OFFSET, 0);
  515. if (IS_ERR(child))
  516. return PTR_ERR(child);
  517. }
  518. if (twl_has_rtc()) {
  519. /*
  520. * REVISIT platform_data here currently might expose the
  521. * "msecure" line ... but for now we just expect board
  522. * setup to tell the chip "it's always ok to SET_TIME".
  523. * Eventually, Linux might become more aware of such
  524. * HW security concerns, and "least privilege".
  525. */
  526. sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
  527. child = add_child(sub_chip_id, "twl_rtc",
  528. NULL, 0,
  529. true, pdata->irq_base + RTC_INTR_OFFSET, 0);
  530. if (IS_ERR(child))
  531. return PTR_ERR(child);
  532. }
  533. if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
  534. static struct regulator_consumer_supply usb1v5 = {
  535. .supply = "usb1v5",
  536. };
  537. static struct regulator_consumer_supply usb1v8 = {
  538. .supply = "usb1v8",
  539. };
  540. static struct regulator_consumer_supply usb3v1 = {
  541. .supply = "usb3v1",
  542. };
  543. /* First add the regulators so that they can be used by transceiver */
  544. if (twl_has_regulator()) {
  545. /* this is a template that gets copied */
  546. struct regulator_init_data usb_fixed = {
  547. .constraints.valid_modes_mask =
  548. REGULATOR_MODE_NORMAL
  549. | REGULATOR_MODE_STANDBY,
  550. .constraints.valid_ops_mask =
  551. REGULATOR_CHANGE_MODE
  552. | REGULATOR_CHANGE_STATUS,
  553. };
  554. child = add_regulator_linked(TWL4030_REG_VUSB1V5,
  555. &usb_fixed, &usb1v5, 1);
  556. if (IS_ERR(child))
  557. return PTR_ERR(child);
  558. child = add_regulator_linked(TWL4030_REG_VUSB1V8,
  559. &usb_fixed, &usb1v8, 1);
  560. if (IS_ERR(child))
  561. return PTR_ERR(child);
  562. child = add_regulator_linked(TWL4030_REG_VUSB3V1,
  563. &usb_fixed, &usb3v1, 1);
  564. if (IS_ERR(child))
  565. return PTR_ERR(child);
  566. }
  567. child = add_child(0, "twl4030_usb",
  568. pdata->usb, sizeof(*pdata->usb),
  569. true,
  570. /* irq0 = USB_PRES, irq1 = USB */
  571. pdata->irq_base + USB_PRES_INTR_OFFSET,
  572. pdata->irq_base + USB_INTR_OFFSET);
  573. if (IS_ERR(child))
  574. return PTR_ERR(child);
  575. /* we need to connect regulators to this transceiver */
  576. if (twl_has_regulator() && child) {
  577. usb1v5.dev = child;
  578. usb1v8.dev = child;
  579. usb3v1.dev = child;
  580. }
  581. }
  582. if (twl_has_watchdog()) {
  583. child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
  584. if (IS_ERR(child))
  585. return PTR_ERR(child);
  586. }
  587. if (twl_has_pwrbutton()) {
  588. child = add_child(1, "twl4030_pwrbutton",
  589. NULL, 0, true, pdata->irq_base + 8 + 0, 0);
  590. if (IS_ERR(child))
  591. return PTR_ERR(child);
  592. }
  593. if (twl_has_codec() && pdata->codec && twl_class_is_4030()) {
  594. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  595. child = add_child(sub_chip_id, "twl4030_codec",
  596. pdata->codec, sizeof(*pdata->codec),
  597. false, 0, 0);
  598. if (IS_ERR(child))
  599. return PTR_ERR(child);
  600. }
  601. /* Phoenix*/
  602. if (twl_has_codec() && pdata->codec && twl_class_is_6030()) {
  603. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  604. child = add_child(sub_chip_id, "twl6030_codec",
  605. pdata->codec, sizeof(*pdata->codec),
  606. false, 0, 0);
  607. if (IS_ERR(child))
  608. return PTR_ERR(child);
  609. }
  610. /* twl4030 regulators */
  611. if (twl_has_regulator() && twl_class_is_4030()) {
  612. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
  613. if (IS_ERR(child))
  614. return PTR_ERR(child);
  615. child = add_regulator(TWL4030_REG_VIO, pdata->vio);
  616. if (IS_ERR(child))
  617. return PTR_ERR(child);
  618. child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1);
  619. if (IS_ERR(child))
  620. return PTR_ERR(child);
  621. child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2);
  622. if (IS_ERR(child))
  623. return PTR_ERR(child);
  624. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
  625. if (IS_ERR(child))
  626. return PTR_ERR(child);
  627. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
  628. if (IS_ERR(child))
  629. return PTR_ERR(child);
  630. child = add_regulator((features & TWL4030_VAUX2)
  631. ? TWL4030_REG_VAUX2_4030
  632. : TWL4030_REG_VAUX2,
  633. pdata->vaux2);
  634. if (IS_ERR(child))
  635. return PTR_ERR(child);
  636. child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1);
  637. if (IS_ERR(child))
  638. return PTR_ERR(child);
  639. child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2);
  640. if (IS_ERR(child))
  641. return PTR_ERR(child);
  642. child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig);
  643. if (IS_ERR(child))
  644. return PTR_ERR(child);
  645. }
  646. /* maybe add LDOs that are omitted on cost-reduced parts */
  647. if (twl_has_regulator() && !(features & TPS_SUBSET)
  648. && twl_class_is_4030()) {
  649. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
  650. if (IS_ERR(child))
  651. return PTR_ERR(child);
  652. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
  653. if (IS_ERR(child))
  654. return PTR_ERR(child);
  655. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
  656. if (IS_ERR(child))
  657. return PTR_ERR(child);
  658. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
  659. if (IS_ERR(child))
  660. return PTR_ERR(child);
  661. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
  662. if (IS_ERR(child))
  663. return PTR_ERR(child);
  664. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
  665. if (IS_ERR(child))
  666. return PTR_ERR(child);
  667. }
  668. /* twl6030 regulators */
  669. if (twl_has_regulator() && twl_class_is_6030()) {
  670. child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc);
  671. if (IS_ERR(child))
  672. return PTR_ERR(child);
  673. child = add_regulator(TWL6030_REG_VPP, pdata->vpp);
  674. if (IS_ERR(child))
  675. return PTR_ERR(child);
  676. child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim);
  677. if (IS_ERR(child))
  678. return PTR_ERR(child);
  679. child = add_regulator(TWL6030_REG_VANA, pdata->vana);
  680. if (IS_ERR(child))
  681. return PTR_ERR(child);
  682. child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio);
  683. if (IS_ERR(child))
  684. return PTR_ERR(child);
  685. child = add_regulator(TWL6030_REG_VDAC, pdata->vdac);
  686. if (IS_ERR(child))
  687. return PTR_ERR(child);
  688. child = add_regulator(TWL6030_REG_VUSB, pdata->vusb);
  689. if (IS_ERR(child))
  690. return PTR_ERR(child);
  691. child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1);
  692. if (IS_ERR(child))
  693. return PTR_ERR(child);
  694. child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2);
  695. if (IS_ERR(child))
  696. return PTR_ERR(child);
  697. child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3);
  698. if (IS_ERR(child))
  699. return PTR_ERR(child);
  700. }
  701. return 0;
  702. }
  703. /*----------------------------------------------------------------------*/
  704. /*
  705. * These three functions initialize the on-chip clock framework,
  706. * letting it generate the right frequencies for USB, MADC, and
  707. * other purposes.
  708. */
  709. static inline int __init protect_pm_master(void)
  710. {
  711. int e = 0;
  712. e = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, KEY_LOCK,
  713. R_PROTECT_KEY);
  714. return e;
  715. }
  716. static inline int __init unprotect_pm_master(void)
  717. {
  718. int e = 0;
  719. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, KEY_UNLOCK1,
  720. R_PROTECT_KEY);
  721. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, KEY_UNLOCK2,
  722. R_PROTECT_KEY);
  723. return e;
  724. }
  725. static void clocks_init(struct device *dev,
  726. struct twl4030_clock_init_data *clock)
  727. {
  728. int e = 0;
  729. struct clk *osc;
  730. u32 rate;
  731. u8 ctrl = HFCLK_FREQ_26_MHZ;
  732. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  733. if (cpu_is_omap2430())
  734. osc = clk_get(dev, "osc_ck");
  735. else
  736. osc = clk_get(dev, "osc_sys_ck");
  737. if (IS_ERR(osc)) {
  738. printk(KERN_WARNING "Skipping twl internal clock init and "
  739. "using bootloader value (unknown osc rate)\n");
  740. return;
  741. }
  742. rate = clk_get_rate(osc);
  743. clk_put(osc);
  744. #else
  745. /* REVISIT for non-OMAP systems, pass the clock rate from
  746. * board init code, using platform_data.
  747. */
  748. osc = ERR_PTR(-EIO);
  749. printk(KERN_WARNING "Skipping twl internal clock init and "
  750. "using bootloader value (unknown osc rate)\n");
  751. return;
  752. #endif
  753. switch (rate) {
  754. case 19200000:
  755. ctrl = HFCLK_FREQ_19p2_MHZ;
  756. break;
  757. case 26000000:
  758. ctrl = HFCLK_FREQ_26_MHZ;
  759. break;
  760. case 38400000:
  761. ctrl = HFCLK_FREQ_38p4_MHZ;
  762. break;
  763. }
  764. ctrl |= HIGH_PERF_SQ;
  765. if (clock && clock->ck32k_lowpwr_enable)
  766. ctrl |= CK32K_LOWPWR_EN;
  767. e |= unprotect_pm_master();
  768. /* effect->MADC+USB ck en */
  769. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  770. e |= protect_pm_master();
  771. if (e < 0)
  772. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  773. }
  774. /*----------------------------------------------------------------------*/
  775. int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  776. int twl4030_exit_irq(void);
  777. int twl4030_init_chip_irq(const char *chip);
  778. int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  779. int twl6030_exit_irq(void);
  780. static int twl_remove(struct i2c_client *client)
  781. {
  782. unsigned i;
  783. int status;
  784. if (twl_class_is_4030())
  785. status = twl4030_exit_irq();
  786. else
  787. status = twl6030_exit_irq();
  788. if (status < 0)
  789. return status;
  790. for (i = 0; i < TWL_NUM_SLAVES; i++) {
  791. struct twl_client *twl = &twl_modules[i];
  792. if (twl->client && twl->client != client)
  793. i2c_unregister_device(twl->client);
  794. twl_modules[i].client = NULL;
  795. }
  796. inuse = false;
  797. return 0;
  798. }
  799. /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
  800. static int __init
  801. twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
  802. {
  803. int status;
  804. unsigned i;
  805. struct twl4030_platform_data *pdata = client->dev.platform_data;
  806. u8 temp;
  807. if (!pdata) {
  808. dev_dbg(&client->dev, "no platform data?\n");
  809. return -EINVAL;
  810. }
  811. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  812. dev_dbg(&client->dev, "can't talk I2C?\n");
  813. return -EIO;
  814. }
  815. if (inuse) {
  816. dev_dbg(&client->dev, "driver is already in use\n");
  817. return -EBUSY;
  818. }
  819. for (i = 0; i < TWL_NUM_SLAVES; i++) {
  820. struct twl_client *twl = &twl_modules[i];
  821. twl->address = client->addr + i;
  822. if (i == 0)
  823. twl->client = client;
  824. else {
  825. twl->client = i2c_new_dummy(client->adapter,
  826. twl->address);
  827. if (!twl->client) {
  828. dev_err(&client->dev,
  829. "can't attach client %d\n", i);
  830. status = -ENOMEM;
  831. goto fail;
  832. }
  833. }
  834. mutex_init(&twl->xfer_lock);
  835. }
  836. inuse = true;
  837. if ((id->driver_data) & TWL6030_CLASS) {
  838. twl_id = TWL6030_CLASS_ID;
  839. twl_map = &twl6030_map[0];
  840. } else {
  841. twl_id = TWL4030_CLASS_ID;
  842. twl_map = &twl4030_map[0];
  843. }
  844. /* setup clock framework */
  845. clocks_init(&client->dev, pdata->clock);
  846. /* load power event scripts */
  847. if (twl_has_power() && pdata->power)
  848. twl4030_power_init(pdata->power);
  849. /* Maybe init the T2 Interrupt subsystem */
  850. if (client->irq
  851. && pdata->irq_base
  852. && pdata->irq_end > pdata->irq_base) {
  853. if (twl_class_is_4030()) {
  854. twl4030_init_chip_irq(id->name);
  855. status = twl4030_init_irq(client->irq, pdata->irq_base,
  856. pdata->irq_end);
  857. } else {
  858. status = twl6030_init_irq(client->irq, pdata->irq_base,
  859. pdata->irq_end);
  860. }
  861. if (status < 0)
  862. goto fail;
  863. }
  864. /* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
  865. * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
  866. * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
  867. */
  868. if (twl_class_is_4030()) {
  869. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
  870. temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
  871. I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
  872. twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
  873. }
  874. status = add_children(pdata, id->driver_data);
  875. fail:
  876. if (status < 0)
  877. twl_remove(client);
  878. return status;
  879. }
  880. static const struct i2c_device_id twl_ids[] = {
  881. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  882. { "twl5030", 0 }, /* T2 updated */
  883. { "twl5031", TWL5031 }, /* TWL5030 updated */
  884. { "tps65950", 0 }, /* catalog version of twl5030 */
  885. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  886. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  887. { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
  888. { /* end of list */ },
  889. };
  890. MODULE_DEVICE_TABLE(i2c, twl_ids);
  891. /* One Client Driver , 4 Clients */
  892. static struct i2c_driver twl_driver = {
  893. .driver.name = DRIVER_NAME,
  894. .id_table = twl_ids,
  895. .probe = twl_probe,
  896. .remove = twl_remove,
  897. };
  898. static int __init twl_init(void)
  899. {
  900. return i2c_add_driver(&twl_driver);
  901. }
  902. subsys_initcall(twl_init);
  903. static void __exit twl_exit(void)
  904. {
  905. i2c_del_driver(&twl_driver);
  906. }
  907. module_exit(twl_exit);
  908. MODULE_AUTHOR("Texas Instruments, Inc.");
  909. MODULE_DESCRIPTION("I2C Core interface for TWL");
  910. MODULE_LICENSE("GPL");