twl-core.c 28 KB

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