twl-core.c 28 KB

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