i2c-stu300.c 26 KB

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