i2c-stu300.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. /*
  2. * Copyright (C) 2007-2009 ST-Ericsson AB
  3. * License terms: GNU General Public License (GPL) version 2
  4. * ST DDC I2C master mode driver, used in e.g. U300 series platforms.
  5. * Author: Linus Walleij <linus.walleij@stericsson.com>
  6. * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/delay.h>
  12. #include <linux/i2c.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/completion.h>
  15. #include <linux/err.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. /* the name of this kernel module */
  21. #define NAME "stu300"
  22. /* CR (Control Register) 8bit (R/W) */
  23. #define I2C_CR (0x00000000)
  24. #define I2C_CR_RESET_VALUE (0x00)
  25. #define I2C_CR_RESET_UMASK (0x00)
  26. #define I2C_CR_DDC1_ENABLE (0x80)
  27. #define I2C_CR_TRANS_ENABLE (0x40)
  28. #define I2C_CR_PERIPHERAL_ENABLE (0x20)
  29. #define I2C_CR_DDC2B_ENABLE (0x10)
  30. #define I2C_CR_START_ENABLE (0x08)
  31. #define I2C_CR_ACK_ENABLE (0x04)
  32. #define I2C_CR_STOP_ENABLE (0x02)
  33. #define I2C_CR_INTERRUPT_ENABLE (0x01)
  34. /* SR1 (Status Register 1) 8bit (R/-) */
  35. #define I2C_SR1 (0x00000004)
  36. #define I2C_SR1_RESET_VALUE (0x00)
  37. #define I2C_SR1_RESET_UMASK (0x00)
  38. #define I2C_SR1_EVF_IND (0x80)
  39. #define I2C_SR1_ADD10_IND (0x40)
  40. #define I2C_SR1_TRA_IND (0x20)
  41. #define I2C_SR1_BUSY_IND (0x10)
  42. #define I2C_SR1_BTF_IND (0x08)
  43. #define I2C_SR1_ADSL_IND (0x04)
  44. #define I2C_SR1_MSL_IND (0x02)
  45. #define I2C_SR1_SB_IND (0x01)
  46. /* SR2 (Status Register 2) 8bit (R/-) */
  47. #define I2C_SR2 (0x00000008)
  48. #define I2C_SR2_RESET_VALUE (0x00)
  49. #define I2C_SR2_RESET_UMASK (0x40)
  50. #define I2C_SR2_MASK (0xBF)
  51. #define I2C_SR2_SCLFAL_IND (0x80)
  52. #define I2C_SR2_ENDAD_IND (0x20)
  53. #define I2C_SR2_AF_IND (0x10)
  54. #define I2C_SR2_STOPF_IND (0x08)
  55. #define I2C_SR2_ARLO_IND (0x04)
  56. #define I2C_SR2_BERR_IND (0x02)
  57. #define I2C_SR2_DDC2BF_IND (0x01)
  58. /* CCR (Clock Control Register) 8bit (R/W) */
  59. #define I2C_CCR (0x0000000C)
  60. #define I2C_CCR_RESET_VALUE (0x00)
  61. #define I2C_CCR_RESET_UMASK (0x00)
  62. #define I2C_CCR_MASK (0xFF)
  63. #define I2C_CCR_FMSM (0x80)
  64. #define I2C_CCR_CC_MASK (0x7F)
  65. /* OAR1 (Own Address Register 1) 8bit (R/W) */
  66. #define I2C_OAR1 (0x00000010)
  67. #define I2C_OAR1_RESET_VALUE (0x00)
  68. #define I2C_OAR1_RESET_UMASK (0x00)
  69. #define I2C_OAR1_ADD_MASK (0xFF)
  70. /* OAR2 (Own Address Register 2) 8bit (R/W) */
  71. #define I2C_OAR2 (0x00000014)
  72. #define I2C_OAR2_RESET_VALUE (0x40)
  73. #define I2C_OAR2_RESET_UMASK (0x19)
  74. #define I2C_OAR2_MASK (0xE6)
  75. #define I2C_OAR2_FR_25_10MHZ (0x00)
  76. #define I2C_OAR2_FR_10_1667MHZ (0x20)
  77. #define I2C_OAR2_FR_1667_2667MHZ (0x40)
  78. #define I2C_OAR2_FR_2667_40MHZ (0x60)
  79. #define I2C_OAR2_FR_40_5333MHZ (0x80)
  80. #define I2C_OAR2_FR_5333_66MHZ (0xA0)
  81. #define I2C_OAR2_FR_66_80MHZ (0xC0)
  82. #define I2C_OAR2_FR_80_100MHZ (0xE0)
  83. #define I2C_OAR2_FR_MASK (0xE0)
  84. #define I2C_OAR2_ADD_MASK (0x06)
  85. /* DR (Data Register) 8bit (R/W) */
  86. #define I2C_DR (0x00000018)
  87. #define I2C_DR_RESET_VALUE (0x00)
  88. #define I2C_DR_RESET_UMASK (0xFF)
  89. #define I2C_DR_D_MASK (0xFF)
  90. /* ECCR (Extended Clock Control Register) 8bit (R/W) */
  91. #define I2C_ECCR (0x0000001C)
  92. #define I2C_ECCR_RESET_VALUE (0x00)
  93. #define I2C_ECCR_RESET_UMASK (0xE0)
  94. #define I2C_ECCR_MASK (0x1F)
  95. #define I2C_ECCR_CC_MASK (0x1F)
  96. /*
  97. * These events are more or less responses to commands
  98. * sent into the hardware, presumably reflecting the state
  99. * of an internal state machine.
  100. */
  101. enum stu300_event {
  102. STU300_EVENT_NONE = 0,
  103. STU300_EVENT_1,
  104. STU300_EVENT_2,
  105. STU300_EVENT_3,
  106. STU300_EVENT_4,
  107. STU300_EVENT_5,
  108. STU300_EVENT_6,
  109. STU300_EVENT_7,
  110. STU300_EVENT_8,
  111. STU300_EVENT_9
  112. };
  113. enum stu300_error {
  114. STU300_ERROR_NONE = 0,
  115. STU300_ERROR_ACKNOWLEDGE_FAILURE,
  116. STU300_ERROR_BUS_ERROR,
  117. STU300_ERROR_ARBITRATION_LOST,
  118. STU300_ERROR_UNKNOWN
  119. };
  120. /* timeout waiting for the controller to respond */
  121. #define STU300_TIMEOUT (msecs_to_jiffies(1000))
  122. /*
  123. * The number of address send athemps tried before giving up.
  124. * If the first one failes it seems like 5 to 8 attempts are required.
  125. */
  126. #define NUM_ADDR_RESEND_ATTEMPTS 12
  127. /* I2C clock speed, in Hz 0-400kHz*/
  128. static unsigned int scl_frequency = 100000;
  129. module_param(scl_frequency, uint, 0644);
  130. /**
  131. * struct stu300_dev - the stu300 driver state holder
  132. * @pdev: parent platform device
  133. * @adapter: corresponding I2C adapter
  134. * @phybase: location of I/O area in memory
  135. * @physize: size of I/O area in memory
  136. * @clk: hardware block clock
  137. * @irq: assigned interrupt line
  138. * @cmd_issue_lock: this locks the following cmd_ variables
  139. * @cmd_complete: acknowledge completion for an I2C command
  140. * @cmd_event: expected event coming in as a response to a command
  141. * @cmd_err: error code as response to a command
  142. * @speed: current bus speed in Hz
  143. * @msg_index: index of current message
  144. * @msg_len: length of current message
  145. */
  146. struct stu300_dev {
  147. struct platform_device *pdev;
  148. struct i2c_adapter adapter;
  149. resource_size_t phybase;
  150. resource_size_t physize;
  151. void __iomem *virtbase;
  152. struct clk *clk;
  153. int irq;
  154. spinlock_t cmd_issue_lock;
  155. struct completion cmd_complete;
  156. enum stu300_event cmd_event;
  157. enum stu300_error cmd_err;
  158. unsigned int speed;
  159. int msg_index;
  160. int msg_len;
  161. };
  162. /* Local forward function declarations */
  163. static int stu300_init_hw(struct stu300_dev *dev);
  164. /*
  165. * The block needs writes in both MSW and LSW in order
  166. * for all data lines to reach their destination.
  167. */
  168. static inline void stu300_wr8(u32 value, void __iomem *address)
  169. {
  170. writel((value << 16) | value, address);
  171. }
  172. /*
  173. * This merely masks off the duplicates which appear
  174. * in bytes 1-3. You _MUST_ use 32-bit bus access on this
  175. * device, else it will not work.
  176. */
  177. static inline u32 stu300_r8(void __iomem *address)
  178. {
  179. return readl(address) & 0x000000FFU;
  180. }
  181. static void stu300_irq_enable(struct stu300_dev *dev)
  182. {
  183. u32 val;
  184. val = stu300_r8(dev->virtbase + I2C_CR);
  185. val |= I2C_CR_INTERRUPT_ENABLE;
  186. /* Twice paranoia (possible HW glitch) */
  187. stu300_wr8(val, dev->virtbase + I2C_CR);
  188. stu300_wr8(val, dev->virtbase + I2C_CR);
  189. }
  190. static void stu300_irq_disable(struct stu300_dev *dev)
  191. {
  192. u32 val;
  193. val = stu300_r8(dev->virtbase + I2C_CR);
  194. val &= ~I2C_CR_INTERRUPT_ENABLE;
  195. /* Twice paranoia (possible HW glitch) */
  196. stu300_wr8(val, dev->virtbase + I2C_CR);
  197. stu300_wr8(val, dev->virtbase + I2C_CR);
  198. }
  199. /*
  200. * Tells whether a certain event or events occurred in
  201. * response to a command. The events represent states in
  202. * the internal state machine of the hardware. The events
  203. * are not very well described in the hardware
  204. * documentation and can only be treated as abstract state
  205. * machine states.
  206. *
  207. * @ret 0 = event has not occurred or unknown error, any
  208. * other value means the correct event occurred or an error.
  209. */
  210. static int stu300_event_occurred(struct stu300_dev *dev,
  211. enum stu300_event mr_event) {
  212. u32 status1;
  213. u32 status2;
  214. /* What event happened? */
  215. status1 = stu300_r8(dev->virtbase + I2C_SR1);
  216. if (!(status1 & I2C_SR1_EVF_IND))
  217. /* No event at all */
  218. return 0;
  219. status2 = stu300_r8(dev->virtbase + I2C_SR2);
  220. /* Block any multiple interrupts */
  221. stu300_irq_disable(dev);
  222. /* Check for errors first */
  223. if (status2 & I2C_SR2_AF_IND) {
  224. dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE;
  225. return 1;
  226. } else if (status2 & I2C_SR2_BERR_IND) {
  227. dev->cmd_err = STU300_ERROR_BUS_ERROR;
  228. return 1;
  229. } else if (status2 & I2C_SR2_ARLO_IND) {
  230. dev->cmd_err = STU300_ERROR_ARBITRATION_LOST;
  231. return 1;
  232. }
  233. switch (mr_event) {
  234. case STU300_EVENT_1:
  235. if (status1 & I2C_SR1_ADSL_IND)
  236. return 1;
  237. break;
  238. case STU300_EVENT_2:
  239. case STU300_EVENT_3:
  240. case STU300_EVENT_7:
  241. case STU300_EVENT_8:
  242. if (status1 & I2C_SR1_BTF_IND) {
  243. return 1;
  244. }
  245. break;
  246. case STU300_EVENT_4:
  247. if (status2 & I2C_SR2_STOPF_IND)
  248. return 1;
  249. break;
  250. case STU300_EVENT_5:
  251. if (status1 & I2C_SR1_SB_IND)
  252. /* Clear start bit */
  253. return 1;
  254. break;
  255. case STU300_EVENT_6:
  256. if (status2 & I2C_SR2_ENDAD_IND) {
  257. /* First check for any errors */
  258. return 1;
  259. }
  260. break;
  261. case STU300_EVENT_9:
  262. if (status1 & I2C_SR1_ADD10_IND)
  263. return 1;
  264. break;
  265. default:
  266. break;
  267. }
  268. /* If we get here, we're on thin ice.
  269. * Here we are in a status where we have
  270. * gotten a response that does not match
  271. * what we requested.
  272. */
  273. dev->cmd_err = STU300_ERROR_UNKNOWN;
  274. dev_err(&dev->pdev->dev,
  275. "Unhandled interrupt! %d sr1: 0x%x sr2: 0x%x\n",
  276. mr_event, status1, status2);
  277. return 0;
  278. }
  279. static irqreturn_t stu300_irh(int irq, void *data)
  280. {
  281. struct stu300_dev *dev = data;
  282. int res;
  283. /* Just make sure that the block is clocked */
  284. clk_enable(dev->clk);
  285. /* See if this was what we were waiting for */
  286. spin_lock(&dev->cmd_issue_lock);
  287. res = stu300_event_occurred(dev, dev->cmd_event);
  288. if (res || dev->cmd_err != STU300_ERROR_NONE)
  289. complete(&dev->cmd_complete);
  290. spin_unlock(&dev->cmd_issue_lock);
  291. clk_disable(dev->clk);
  292. return IRQ_HANDLED;
  293. }
  294. /*
  295. * Sends a command and then waits for the bits masked by *flagmask*
  296. * to go high or low by IRQ awaiting.
  297. */
  298. static int stu300_start_and_await_event(struct stu300_dev *dev,
  299. u8 cr_value,
  300. enum stu300_event mr_event)
  301. {
  302. int ret;
  303. if (unlikely(irqs_disabled())) {
  304. /* TODO: implement polling for this case if need be. */
  305. WARN(1, "irqs are disabled, cannot poll for event\n");
  306. return -EIO;
  307. }
  308. /* Lock command issue, fill in an event we wait for */
  309. spin_lock_irq(&dev->cmd_issue_lock);
  310. init_completion(&dev->cmd_complete);
  311. dev->cmd_err = STU300_ERROR_NONE;
  312. dev->cmd_event = mr_event;
  313. spin_unlock_irq(&dev->cmd_issue_lock);
  314. /* Turn on interrupt, send command and wait. */
  315. cr_value |= I2C_CR_INTERRUPT_ENABLE;
  316. stu300_wr8(cr_value, dev->virtbase + I2C_CR);
  317. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  318. STU300_TIMEOUT);
  319. if (ret < 0) {
  320. dev_err(&dev->pdev->dev,
  321. "wait_for_completion_interruptible_timeout() "
  322. "returned %d waiting for event %04x\n", ret, mr_event);
  323. return ret;
  324. }
  325. if (ret == 0) {
  326. dev_err(&dev->pdev->dev, "controller timed out "
  327. "waiting for event %d, reinit hardware\n", mr_event);
  328. (void) stu300_init_hw(dev);
  329. return -ETIMEDOUT;
  330. }
  331. if (dev->cmd_err != STU300_ERROR_NONE) {
  332. dev_err(&dev->pdev->dev, "controller (start) "
  333. "error %d waiting for event %d, reinit hardware\n",
  334. dev->cmd_err, mr_event);
  335. (void) stu300_init_hw(dev);
  336. return -EIO;
  337. }
  338. return 0;
  339. }
  340. /*
  341. * This waits for a flag to be set, if it is not set on entry, an interrupt is
  342. * configured to wait for the flag using a completion.
  343. */
  344. static int stu300_await_event(struct stu300_dev *dev,
  345. enum stu300_event mr_event)
  346. {
  347. int ret;
  348. if (unlikely(irqs_disabled())) {
  349. /* TODO: implement polling for this case if need be. */
  350. dev_err(&dev->pdev->dev, "irqs are disabled on this "
  351. "system!\n");
  352. return -EIO;
  353. }
  354. /* Is it already here? */
  355. spin_lock_irq(&dev->cmd_issue_lock);
  356. dev->cmd_err = STU300_ERROR_NONE;
  357. dev->cmd_event = mr_event;
  358. init_completion(&dev->cmd_complete);
  359. /* Turn on the I2C interrupt for current operation */
  360. stu300_irq_enable(dev);
  361. /* Unlock the command block and wait for the event to occur */
  362. spin_unlock_irq(&dev->cmd_issue_lock);
  363. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  364. STU300_TIMEOUT);
  365. if (ret < 0) {
  366. dev_err(&dev->pdev->dev,
  367. "wait_for_completion_interruptible_timeout()"
  368. "returned %d waiting for event %04x\n", ret, mr_event);
  369. return ret;
  370. }
  371. if (ret == 0) {
  372. if (mr_event != STU300_EVENT_6) {
  373. dev_err(&dev->pdev->dev, "controller "
  374. "timed out waiting for event %d, reinit "
  375. "hardware\n", mr_event);
  376. (void) stu300_init_hw(dev);
  377. }
  378. return -ETIMEDOUT;
  379. }
  380. if (dev->cmd_err != STU300_ERROR_NONE) {
  381. if (mr_event != STU300_EVENT_6) {
  382. dev_err(&dev->pdev->dev, "controller "
  383. "error (await_event) %d waiting for event %d, "
  384. "reinit hardware\n", dev->cmd_err, mr_event);
  385. (void) stu300_init_hw(dev);
  386. }
  387. return -EIO;
  388. }
  389. return 0;
  390. }
  391. /*
  392. * Waits for the busy bit to go low by repeated polling.
  393. */
  394. #define BUSY_RELEASE_ATTEMPTS 10
  395. static int stu300_wait_while_busy(struct stu300_dev *dev)
  396. {
  397. unsigned long timeout;
  398. int i;
  399. for (i = 0; i < BUSY_RELEASE_ATTEMPTS; i++) {
  400. timeout = jiffies + STU300_TIMEOUT;
  401. while (!time_after(jiffies, timeout)) {
  402. /* Is not busy? */
  403. if ((stu300_r8(dev->virtbase + I2C_SR1) &
  404. I2C_SR1_BUSY_IND) == 0)
  405. return 0;
  406. msleep(1);
  407. }
  408. dev_err(&dev->pdev->dev, "transaction timed out "
  409. "waiting for device to be free (not busy). "
  410. "Attempt: %d\n", i+1);
  411. dev_err(&dev->pdev->dev, "base address = "
  412. "0x%08x, reinit hardware\n", (u32) dev->virtbase);
  413. (void) stu300_init_hw(dev);
  414. }
  415. dev_err(&dev->pdev->dev, "giving up after %d attempts "
  416. "to reset the bus.\n", BUSY_RELEASE_ATTEMPTS);
  417. return -ETIMEDOUT;
  418. }
  419. struct stu300_clkset {
  420. unsigned long rate;
  421. u32 setting;
  422. };
  423. static const struct stu300_clkset stu300_clktable[] = {
  424. { 0, 0xFFU },
  425. { 2500000, I2C_OAR2_FR_25_10MHZ },
  426. { 10000000, I2C_OAR2_FR_10_1667MHZ },
  427. { 16670000, I2C_OAR2_FR_1667_2667MHZ },
  428. { 26670000, I2C_OAR2_FR_2667_40MHZ },
  429. { 40000000, I2C_OAR2_FR_40_5333MHZ },
  430. { 53330000, I2C_OAR2_FR_5333_66MHZ },
  431. { 66000000, I2C_OAR2_FR_66_80MHZ },
  432. { 80000000, I2C_OAR2_FR_80_100MHZ },
  433. { 100000000, 0xFFU },
  434. };
  435. static int stu300_set_clk(struct stu300_dev *dev, unsigned long clkrate)
  436. {
  437. u32 val;
  438. int i = 0;
  439. /* Locate the appropriate clock setting */
  440. while (i < ARRAY_SIZE(stu300_clktable) - 1 &&
  441. stu300_clktable[i].rate < clkrate)
  442. i++;
  443. if (stu300_clktable[i].setting == 0xFFU) {
  444. dev_err(&dev->pdev->dev, "too %s clock rate requested "
  445. "(%lu Hz).\n", i ? "high" : "low", clkrate);
  446. return -EINVAL;
  447. }
  448. stu300_wr8(stu300_clktable[i].setting,
  449. dev->virtbase + I2C_OAR2);
  450. dev_dbg(&dev->pdev->dev, "Clock rate %lu Hz, I2C bus speed %d Hz "
  451. "virtbase %p\n", clkrate, dev->speed, dev->virtbase);
  452. if (dev->speed > 100000)
  453. /* Fast Mode I2C */
  454. val = ((clkrate/dev->speed) - 9)/3 + 1;
  455. else
  456. /* Standard Mode I2C */
  457. val = ((clkrate/dev->speed) - 7)/2 + 1;
  458. /* According to spec the divider must be > 2 */
  459. if (val < 0x002) {
  460. dev_err(&dev->pdev->dev, "too low clock rate (%lu Hz).\n",
  461. clkrate);
  462. return -EINVAL;
  463. }
  464. /* We have 12 bits clock divider only! */
  465. if (val & 0xFFFFF000U) {
  466. dev_err(&dev->pdev->dev, "too high clock rate (%lu Hz).\n",
  467. clkrate);
  468. return -EINVAL;
  469. }
  470. if (dev->speed > 100000) {
  471. /* CC6..CC0 */
  472. stu300_wr8((val & I2C_CCR_CC_MASK) | I2C_CCR_FMSM,
  473. dev->virtbase + I2C_CCR);
  474. dev_dbg(&dev->pdev->dev, "set clock divider to 0x%08x, "
  475. "Fast Mode I2C\n", val);
  476. } else {
  477. /* CC6..CC0 */
  478. stu300_wr8((val & I2C_CCR_CC_MASK),
  479. dev->virtbase + I2C_CCR);
  480. dev_dbg(&dev->pdev->dev, "set clock divider to "
  481. "0x%08x, Standard Mode I2C\n", val);
  482. }
  483. /* CC11..CC7 */
  484. stu300_wr8(((val >> 7) & 0x1F),
  485. dev->virtbase + I2C_ECCR);
  486. return 0;
  487. }
  488. static int stu300_init_hw(struct stu300_dev *dev)
  489. {
  490. u32 dummy;
  491. unsigned long clkrate;
  492. int ret;
  493. /* Disable controller */
  494. stu300_wr8(0x00, dev->virtbase + I2C_CR);
  495. /*
  496. * Set own address to some default value (0x00).
  497. * We do not support slave mode anyway.
  498. */
  499. stu300_wr8(0x00, dev->virtbase + I2C_OAR1);
  500. /*
  501. * The I2C controller only operates properly in 26 MHz but we
  502. * program this driver as if we didn't know. This will also set the two
  503. * high bits of the own address to zero as well.
  504. * There is no known hardware issue with running in 13 MHz
  505. * However, speeds over 200 kHz are not used.
  506. */
  507. clkrate = clk_get_rate(dev->clk);
  508. ret = stu300_set_clk(dev, clkrate);
  509. if (ret)
  510. return ret;
  511. /*
  512. * Enable block, do it TWICE (hardware glitch)
  513. * Setting bit 7 can enable DDC mode. (Not used currently.)
  514. */
  515. stu300_wr8(I2C_CR_PERIPHERAL_ENABLE,
  516. dev->virtbase + I2C_CR);
  517. stu300_wr8(I2C_CR_PERIPHERAL_ENABLE,
  518. dev->virtbase + I2C_CR);
  519. /* Make a dummy read of the status register SR1 & SR2 */
  520. dummy = stu300_r8(dev->virtbase + I2C_SR2);
  521. dummy = stu300_r8(dev->virtbase + I2C_SR1);
  522. return 0;
  523. }
  524. /* Send slave address. */
  525. static int stu300_send_address(struct stu300_dev *dev,
  526. struct i2c_msg *msg, int resend)
  527. {
  528. u32 val;
  529. int ret;
  530. if (msg->flags & I2C_M_TEN)
  531. /* This is probably how 10 bit addresses look */
  532. val = (0xf0 | (((u32) msg->addr & 0x300) >> 7)) &
  533. I2C_DR_D_MASK;
  534. else
  535. val = ((msg->addr << 1) & I2C_DR_D_MASK);
  536. if (msg->flags & I2C_M_RD) {
  537. /* This is the direction bit */
  538. val |= 0x01;
  539. if (resend)
  540. dev_dbg(&dev->pdev->dev, "read resend\n");
  541. } else if (resend)
  542. dev_dbg(&dev->pdev->dev, "write resend\n");
  543. stu300_wr8(val, dev->virtbase + I2C_DR);
  544. /* For 10bit addressing, await 10bit request (EVENT 9) */
  545. if (msg->flags & I2C_M_TEN) {
  546. ret = stu300_await_event(dev, STU300_EVENT_9);
  547. /*
  548. * The slave device wants a 10bit address, send the rest
  549. * of the bits (the LSBits)
  550. */
  551. val = msg->addr & I2C_DR_D_MASK;
  552. /* This clears "event 9" */
  553. stu300_wr8(val, dev->virtbase + I2C_DR);
  554. if (ret != 0)
  555. return ret;
  556. }
  557. /* FIXME: Why no else here? two events for 10bit?
  558. * Await event 6 (normal) or event 9 (10bit)
  559. */
  560. if (resend)
  561. dev_dbg(&dev->pdev->dev, "await event 6\n");
  562. ret = stu300_await_event(dev, STU300_EVENT_6);
  563. /*
  564. * Clear any pending EVENT 6 no matter what happened during
  565. * await_event.
  566. */
  567. val = stu300_r8(dev->virtbase + I2C_CR);
  568. val |= I2C_CR_PERIPHERAL_ENABLE;
  569. stu300_wr8(val, dev->virtbase + I2C_CR);
  570. return ret;
  571. }
  572. static int stu300_xfer_msg(struct i2c_adapter *adap,
  573. struct i2c_msg *msg, int stop)
  574. {
  575. u32 cr;
  576. u32 val;
  577. u32 i;
  578. int ret;
  579. int attempts = 0;
  580. struct stu300_dev *dev = i2c_get_adapdata(adap);
  581. clk_enable(dev->clk);
  582. /* Remove this if (0) to trace each and every message. */
  583. if (0) {
  584. dev_dbg(&dev->pdev->dev, "I2C message to: 0x%04x, len: %d, "
  585. "flags: 0x%04x, stop: %d\n",
  586. msg->addr, msg->len, msg->flags, stop);
  587. }
  588. /* Zero-length messages are not supported by this hardware */
  589. if (msg->len == 0) {
  590. ret = -EINVAL;
  591. goto exit_disable;
  592. }
  593. /*
  594. * For some reason, sending the address sometimes fails when running
  595. * on the 13 MHz clock. No interrupt arrives. This is a work around,
  596. * which tries to restart and send the address up to 10 times before
  597. * really giving up. Usually 5 to 8 attempts are enough.
  598. */
  599. do {
  600. if (attempts)
  601. dev_dbg(&dev->pdev->dev, "wait while busy\n");
  602. /* Check that the bus is free, or wait until some timeout */
  603. ret = stu300_wait_while_busy(dev);
  604. if (ret != 0)
  605. goto exit_disable;
  606. if (attempts)
  607. dev_dbg(&dev->pdev->dev, "re-int hw\n");
  608. /*
  609. * According to ST, there is no problem if the clock is
  610. * changed between 13 and 26 MHz during a transfer.
  611. */
  612. ret = stu300_init_hw(dev);
  613. if (ret)
  614. goto exit_disable;
  615. /* Send a start condition */
  616. cr = I2C_CR_PERIPHERAL_ENABLE;
  617. /* Setting the START bit puts the block in master mode */
  618. if (!(msg->flags & I2C_M_NOSTART))
  619. cr |= I2C_CR_START_ENABLE;
  620. if ((msg->flags & I2C_M_RD) && (msg->len > 1))
  621. /* On read more than 1 byte, we need ack. */
  622. cr |= I2C_CR_ACK_ENABLE;
  623. /* Check that it gets through */
  624. if (!(msg->flags & I2C_M_NOSTART)) {
  625. if (attempts)
  626. dev_dbg(&dev->pdev->dev, "send start event\n");
  627. ret = stu300_start_and_await_event(dev, cr,
  628. STU300_EVENT_5);
  629. }
  630. if (attempts)
  631. dev_dbg(&dev->pdev->dev, "send address\n");
  632. if (ret == 0)
  633. /* Send address */
  634. ret = stu300_send_address(dev, msg, attempts != 0);
  635. if (ret != 0) {
  636. attempts++;
  637. dev_dbg(&dev->pdev->dev, "failed sending address, "
  638. "retrying. Attempt: %d msg_index: %d/%d\n",
  639. attempts, dev->msg_index, dev->msg_len);
  640. }
  641. } while (ret != 0 && attempts < NUM_ADDR_RESEND_ATTEMPTS);
  642. if (attempts < NUM_ADDR_RESEND_ATTEMPTS && attempts > 0) {
  643. dev_dbg(&dev->pdev->dev, "managed to get address "
  644. "through after %d attempts\n", attempts);
  645. } else if (attempts == NUM_ADDR_RESEND_ATTEMPTS) {
  646. dev_dbg(&dev->pdev->dev, "I give up, tried %d times "
  647. "to resend address.\n",
  648. NUM_ADDR_RESEND_ATTEMPTS);
  649. goto exit_disable;
  650. }
  651. if (msg->flags & I2C_M_RD) {
  652. /* READ: we read the actual bytes one at a time */
  653. for (i = 0; i < msg->len; i++) {
  654. if (i == msg->len-1) {
  655. /*
  656. * Disable ACK and set STOP condition before
  657. * reading last byte
  658. */
  659. val = I2C_CR_PERIPHERAL_ENABLE;
  660. if (stop)
  661. val |= I2C_CR_STOP_ENABLE;
  662. stu300_wr8(val,
  663. dev->virtbase + I2C_CR);
  664. }
  665. /* Wait for this byte... */
  666. ret = stu300_await_event(dev, STU300_EVENT_7);
  667. if (ret != 0)
  668. goto exit_disable;
  669. /* This clears event 7 */
  670. msg->buf[i] = (u8) stu300_r8(dev->virtbase + I2C_DR);
  671. }
  672. } else {
  673. /* WRITE: we send the actual bytes one at a time */
  674. for (i = 0; i < msg->len; i++) {
  675. /* Write the byte */
  676. stu300_wr8(msg->buf[i],
  677. dev->virtbase + I2C_DR);
  678. /* Check status */
  679. ret = stu300_await_event(dev, STU300_EVENT_8);
  680. /* Next write to DR will clear event 8 */
  681. if (ret != 0) {
  682. dev_err(&dev->pdev->dev, "error awaiting "
  683. "event 8 (%d)\n", ret);
  684. goto exit_disable;
  685. }
  686. }
  687. /* Check NAK */
  688. if (!(msg->flags & I2C_M_IGNORE_NAK)) {
  689. if (stu300_r8(dev->virtbase + I2C_SR2) &
  690. I2C_SR2_AF_IND) {
  691. dev_err(&dev->pdev->dev, "I2C payload "
  692. "send returned NAK!\n");
  693. ret = -EIO;
  694. goto exit_disable;
  695. }
  696. }
  697. if (stop) {
  698. /* Send stop condition */
  699. val = I2C_CR_PERIPHERAL_ENABLE;
  700. val |= I2C_CR_STOP_ENABLE;
  701. stu300_wr8(val, dev->virtbase + I2C_CR);
  702. }
  703. }
  704. /* Check that the bus is free, or wait until some timeout occurs */
  705. ret = stu300_wait_while_busy(dev);
  706. if (ret != 0) {
  707. dev_err(&dev->pdev->dev, "timout waiting for transfer "
  708. "to commence.\n");
  709. goto exit_disable;
  710. }
  711. /* Dummy read status registers */
  712. val = stu300_r8(dev->virtbase + I2C_SR2);
  713. val = stu300_r8(dev->virtbase + I2C_SR1);
  714. ret = 0;
  715. exit_disable:
  716. /* Disable controller */
  717. stu300_wr8(0x00, dev->virtbase + I2C_CR);
  718. clk_disable(dev->clk);
  719. return ret;
  720. }
  721. static int stu300_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  722. int num)
  723. {
  724. int ret = -1;
  725. int i;
  726. struct stu300_dev *dev = i2c_get_adapdata(adap);
  727. dev->msg_len = num;
  728. for (i = 0; i < num; i++) {
  729. /*
  730. * Another driver appears to send stop for each message,
  731. * here we only do that for the last message. Possibly some
  732. * peripherals require this behaviour, then their drivers
  733. * have to send single messages in order to get "stop" for
  734. * each message.
  735. */
  736. dev->msg_index = i;
  737. ret = stu300_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  738. if (ret != 0) {
  739. num = ret;
  740. break;
  741. }
  742. }
  743. return num;
  744. }
  745. static u32 stu300_func(struct i2c_adapter *adap)
  746. {
  747. /* This is the simplest thing you can think of... */
  748. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
  749. }
  750. static const struct i2c_algorithm stu300_algo = {
  751. .master_xfer = stu300_xfer,
  752. .functionality = stu300_func,
  753. };
  754. static int __init
  755. stu300_probe(struct platform_device *pdev)
  756. {
  757. struct stu300_dev *dev;
  758. struct i2c_adapter *adap;
  759. struct resource *res;
  760. int bus_nr;
  761. int ret = 0;
  762. char clk_name[] = "I2C0";
  763. dev = kzalloc(sizeof(struct stu300_dev), GFP_KERNEL);
  764. if (!dev) {
  765. dev_err(&pdev->dev, "could not allocate device struct\n");
  766. ret = -ENOMEM;
  767. goto err_no_devmem;
  768. }
  769. bus_nr = pdev->id;
  770. clk_name[3] += (char)bus_nr;
  771. dev->clk = clk_get(&pdev->dev, clk_name);
  772. if (IS_ERR(dev->clk)) {
  773. ret = PTR_ERR(dev->clk);
  774. dev_err(&pdev->dev, "could not retrieve i2c bus clock\n");
  775. goto err_no_clk;
  776. }
  777. dev->pdev = pdev;
  778. platform_set_drvdata(pdev, dev);
  779. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  780. if (!res) {
  781. ret = -ENOENT;
  782. goto err_no_resource;
  783. }
  784. dev->phybase = res->start;
  785. dev->physize = resource_size(res);
  786. if (request_mem_region(dev->phybase, dev->physize,
  787. NAME " I/O Area") == NULL) {
  788. ret = -EBUSY;
  789. goto err_no_ioregion;
  790. }
  791. dev->virtbase = ioremap(dev->phybase, dev->physize);
  792. dev_dbg(&pdev->dev, "initialize bus device I2C%d on virtual "
  793. "base %p\n", bus_nr, dev->virtbase);
  794. if (!dev->virtbase) {
  795. ret = -ENOMEM;
  796. goto err_no_ioremap;
  797. }
  798. dev->irq = platform_get_irq(pdev, 0);
  799. if (request_irq(dev->irq, stu300_irh, 0,
  800. NAME, dev)) {
  801. ret = -EIO;
  802. goto err_no_irq;
  803. }
  804. dev->speed = scl_frequency;
  805. clk_enable(dev->clk);
  806. ret = stu300_init_hw(dev);
  807. clk_disable(dev->clk);
  808. if (ret != 0) {
  809. dev_err(&dev->pdev->dev, "error initializing hardware.\n");
  810. goto err_init_hw;
  811. }
  812. /* IRQ event handling initialization */
  813. spin_lock_init(&dev->cmd_issue_lock);
  814. dev->cmd_event = STU300_EVENT_NONE;
  815. dev->cmd_err = STU300_ERROR_NONE;
  816. adap = &dev->adapter;
  817. adap->owner = THIS_MODULE;
  818. /* DDC class but actually often used for more generic I2C */
  819. adap->class = I2C_CLASS_DDC;
  820. strlcpy(adap->name, "ST Microelectronics DDC I2C adapter",
  821. sizeof(adap->name));
  822. adap->nr = bus_nr;
  823. adap->algo = &stu300_algo;
  824. adap->dev.parent = &pdev->dev;
  825. i2c_set_adapdata(adap, dev);
  826. /* i2c device drivers may be active on return from add_adapter() */
  827. ret = i2c_add_numbered_adapter(adap);
  828. if (ret) {
  829. dev_err(&dev->pdev->dev, "failure adding ST Micro DDC "
  830. "I2C adapter\n");
  831. goto err_add_adapter;
  832. }
  833. return 0;
  834. err_add_adapter:
  835. err_init_hw:
  836. free_irq(dev->irq, dev);
  837. err_no_irq:
  838. iounmap(dev->virtbase);
  839. err_no_ioremap:
  840. release_mem_region(dev->phybase, dev->physize);
  841. err_no_ioregion:
  842. platform_set_drvdata(pdev, NULL);
  843. err_no_resource:
  844. clk_put(dev->clk);
  845. err_no_clk:
  846. kfree(dev);
  847. err_no_devmem:
  848. dev_err(&pdev->dev, "failed to add " NAME " adapter: %d\n",
  849. pdev->id);
  850. return ret;
  851. }
  852. #ifdef CONFIG_PM
  853. static int stu300_suspend(struct platform_device *pdev, pm_message_t state)
  854. {
  855. struct stu300_dev *dev = platform_get_drvdata(pdev);
  856. /* Turn off everything */
  857. stu300_wr8(0x00, dev->virtbase + I2C_CR);
  858. return 0;
  859. }
  860. static int stu300_resume(struct platform_device *pdev)
  861. {
  862. int ret = 0;
  863. struct stu300_dev *dev = platform_get_drvdata(pdev);
  864. clk_enable(dev->clk);
  865. ret = stu300_init_hw(dev);
  866. clk_disable(dev->clk);
  867. if (ret != 0)
  868. dev_err(&pdev->dev, "error re-initializing hardware.\n");
  869. return ret;
  870. }
  871. #else
  872. #define stu300_suspend NULL
  873. #define stu300_resume NULL
  874. #endif
  875. static int __exit
  876. stu300_remove(struct platform_device *pdev)
  877. {
  878. struct stu300_dev *dev = platform_get_drvdata(pdev);
  879. i2c_del_adapter(&dev->adapter);
  880. /* Turn off everything */
  881. stu300_wr8(0x00, dev->virtbase + I2C_CR);
  882. free_irq(dev->irq, dev);
  883. iounmap(dev->virtbase);
  884. release_mem_region(dev->phybase, dev->physize);
  885. clk_put(dev->clk);
  886. platform_set_drvdata(pdev, NULL);
  887. kfree(dev);
  888. return 0;
  889. }
  890. static struct platform_driver stu300_i2c_driver = {
  891. .driver = {
  892. .name = NAME,
  893. .owner = THIS_MODULE,
  894. },
  895. .remove = __exit_p(stu300_remove),
  896. .suspend = stu300_suspend,
  897. .resume = stu300_resume,
  898. };
  899. static int __init stu300_init(void)
  900. {
  901. return platform_driver_probe(&stu300_i2c_driver, stu300_probe);
  902. }
  903. static void __exit stu300_exit(void)
  904. {
  905. platform_driver_unregister(&stu300_i2c_driver);
  906. }
  907. /*
  908. * The systems using this bus often have very basic devices such
  909. * as regulators on the I2C bus, so this needs to be loaded early.
  910. * Therefore it is registered in the subsys_initcall().
  911. */
  912. subsys_initcall(stu300_init);
  913. module_exit(stu300_exit);
  914. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  915. MODULE_DESCRIPTION("ST Micro DDC I2C adapter (" NAME ")");
  916. MODULE_LICENSE("GPL");
  917. MODULE_ALIAS("platform:" NAME);