i2c-stu300.c 26 KB

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