low_i2c.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*
  2. * arch/powerpc/platforms/powermac/low_i2c.c
  3. *
  4. * Copyright (C) 2003-2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * The linux i2c layer isn't completely suitable for our needs for various
  12. * reasons ranging from too late initialisation to semantics not perfectly
  13. * matching some requirements of the apple platform functions etc...
  14. *
  15. * This file thus provides a simple low level unified i2c interface for
  16. * powermac that covers the various types of i2c busses used in Apple machines.
  17. * For now, keywest, PMU and SMU, though we could add Cuda, or other bit
  18. * banging busses found on older chipstes in earlier machines if we ever need
  19. * one of them.
  20. *
  21. * The drivers in this file are synchronous/blocking. In addition, the
  22. * keywest one is fairly slow due to the use of msleep instead of interrupts
  23. * as the interrupt is currently used by i2c-keywest. In the long run, we
  24. * might want to get rid of those high-level interfaces to linux i2c layer
  25. * either completely (converting all drivers) or replacing them all with a
  26. * single stub driver on top of this one. Once done, the interrupt will be
  27. * available for our use.
  28. */
  29. #undef DEBUG
  30. #undef DEBUG_LOW
  31. #include <linux/types.h>
  32. #include <linux/sched.h>
  33. #include <linux/init.h>
  34. #include <linux/module.h>
  35. #include <linux/adb.h>
  36. #include <linux/pmu.h>
  37. #include <linux/delay.h>
  38. #include <linux/completion.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/timer.h>
  42. #include <linux/mutex.h>
  43. #include <linux/i2c.h>
  44. #include <asm/keylargo.h>
  45. #include <asm/uninorth.h>
  46. #include <asm/io.h>
  47. #include <asm/prom.h>
  48. #include <asm/machdep.h>
  49. #include <asm/smu.h>
  50. #include <asm/pmac_pfunc.h>
  51. #include <asm/pmac_low_i2c.h>
  52. #ifdef DEBUG
  53. #define DBG(x...) do {\
  54. printk(KERN_DEBUG "low_i2c:" x); \
  55. } while(0)
  56. #else
  57. #define DBG(x...)
  58. #endif
  59. #ifdef DEBUG_LOW
  60. #define DBG_LOW(x...) do {\
  61. printk(KERN_DEBUG "low_i2c:" x); \
  62. } while(0)
  63. #else
  64. #define DBG_LOW(x...)
  65. #endif
  66. static int pmac_i2c_force_poll = 1;
  67. /*
  68. * A bus structure. Each bus in the system has such a structure associated.
  69. */
  70. struct pmac_i2c_bus
  71. {
  72. struct list_head link;
  73. struct device_node *controller;
  74. struct device_node *busnode;
  75. int type;
  76. int flags;
  77. struct i2c_adapter adapter;
  78. void *hostdata;
  79. int channel; /* some hosts have multiple */
  80. int mode; /* current mode */
  81. struct mutex mutex;
  82. int opened;
  83. int polled; /* open mode */
  84. struct platform_device *platform_dev;
  85. /* ops */
  86. int (*open)(struct pmac_i2c_bus *bus);
  87. void (*close)(struct pmac_i2c_bus *bus);
  88. int (*xfer)(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  89. u32 subaddr, u8 *data, int len);
  90. };
  91. static LIST_HEAD(pmac_i2c_busses);
  92. /*
  93. * Keywest implementation
  94. */
  95. struct pmac_i2c_host_kw
  96. {
  97. struct mutex mutex; /* Access mutex for use by
  98. * i2c-keywest */
  99. void __iomem *base; /* register base address */
  100. int bsteps; /* register stepping */
  101. int speed; /* speed */
  102. int irq;
  103. u8 *data;
  104. unsigned len;
  105. int state;
  106. int rw;
  107. int polled;
  108. int result;
  109. struct completion complete;
  110. spinlock_t lock;
  111. struct timer_list timeout_timer;
  112. };
  113. /* Register indices */
  114. typedef enum {
  115. reg_mode = 0,
  116. reg_control,
  117. reg_status,
  118. reg_isr,
  119. reg_ier,
  120. reg_addr,
  121. reg_subaddr,
  122. reg_data
  123. } reg_t;
  124. /* The Tumbler audio equalizer can be really slow sometimes */
  125. #define KW_POLL_TIMEOUT (2*HZ)
  126. /* Mode register */
  127. #define KW_I2C_MODE_100KHZ 0x00
  128. #define KW_I2C_MODE_50KHZ 0x01
  129. #define KW_I2C_MODE_25KHZ 0x02
  130. #define KW_I2C_MODE_DUMB 0x00
  131. #define KW_I2C_MODE_STANDARD 0x04
  132. #define KW_I2C_MODE_STANDARDSUB 0x08
  133. #define KW_I2C_MODE_COMBINED 0x0C
  134. #define KW_I2C_MODE_MODE_MASK 0x0C
  135. #define KW_I2C_MODE_CHAN_MASK 0xF0
  136. /* Control register */
  137. #define KW_I2C_CTL_AAK 0x01
  138. #define KW_I2C_CTL_XADDR 0x02
  139. #define KW_I2C_CTL_STOP 0x04
  140. #define KW_I2C_CTL_START 0x08
  141. /* Status register */
  142. #define KW_I2C_STAT_BUSY 0x01
  143. #define KW_I2C_STAT_LAST_AAK 0x02
  144. #define KW_I2C_STAT_LAST_RW 0x04
  145. #define KW_I2C_STAT_SDA 0x08
  146. #define KW_I2C_STAT_SCL 0x10
  147. /* IER & ISR registers */
  148. #define KW_I2C_IRQ_DATA 0x01
  149. #define KW_I2C_IRQ_ADDR 0x02
  150. #define KW_I2C_IRQ_STOP 0x04
  151. #define KW_I2C_IRQ_START 0x08
  152. #define KW_I2C_IRQ_MASK 0x0F
  153. /* State machine states */
  154. enum {
  155. state_idle,
  156. state_addr,
  157. state_read,
  158. state_write,
  159. state_stop,
  160. state_dead
  161. };
  162. #define WRONG_STATE(name) do {\
  163. printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s " \
  164. "(isr: %02x)\n", \
  165. name, __kw_state_names[host->state], isr); \
  166. } while(0)
  167. static const char *__kw_state_names[] = {
  168. "state_idle",
  169. "state_addr",
  170. "state_read",
  171. "state_write",
  172. "state_stop",
  173. "state_dead"
  174. };
  175. static inline u8 __kw_read_reg(struct pmac_i2c_host_kw *host, reg_t reg)
  176. {
  177. return readb(host->base + (((unsigned int)reg) << host->bsteps));
  178. }
  179. static inline void __kw_write_reg(struct pmac_i2c_host_kw *host,
  180. reg_t reg, u8 val)
  181. {
  182. writeb(val, host->base + (((unsigned)reg) << host->bsteps));
  183. (void)__kw_read_reg(host, reg_subaddr);
  184. }
  185. #define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)
  186. #define kw_read_reg(reg) __kw_read_reg(host, reg)
  187. static u8 kw_i2c_wait_interrupt(struct pmac_i2c_host_kw *host)
  188. {
  189. int i, j;
  190. u8 isr;
  191. for (i = 0; i < 1000; i++) {
  192. isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;
  193. if (isr != 0)
  194. return isr;
  195. /* This code is used with the timebase frozen, we cannot rely
  196. * on udelay nor schedule when in polled mode !
  197. * For now, just use a bogus loop....
  198. */
  199. if (host->polled) {
  200. for (j = 1; j < 100000; j++)
  201. mb();
  202. } else
  203. msleep(1);
  204. }
  205. return isr;
  206. }
  207. static void kw_i2c_do_stop(struct pmac_i2c_host_kw *host, int result)
  208. {
  209. kw_write_reg(reg_control, KW_I2C_CTL_STOP);
  210. host->state = state_stop;
  211. host->result = result;
  212. }
  213. static void kw_i2c_handle_interrupt(struct pmac_i2c_host_kw *host, u8 isr)
  214. {
  215. u8 ack;
  216. DBG_LOW("kw_handle_interrupt(%s, isr: %x)\n",
  217. __kw_state_names[host->state], isr);
  218. if (host->state == state_idle) {
  219. printk(KERN_WARNING "low_i2c: Keywest got an out of state"
  220. " interrupt, ignoring\n");
  221. kw_write_reg(reg_isr, isr);
  222. return;
  223. }
  224. if (isr == 0) {
  225. printk(KERN_WARNING "low_i2c: Timeout in i2c transfer"
  226. " on keywest !\n");
  227. if (host->state != state_stop) {
  228. kw_i2c_do_stop(host, -EIO);
  229. return;
  230. }
  231. ack = kw_read_reg(reg_status);
  232. if (ack & KW_I2C_STAT_BUSY)
  233. kw_write_reg(reg_status, 0);
  234. host->state = state_idle;
  235. kw_write_reg(reg_ier, 0x00);
  236. if (!host->polled)
  237. complete(&host->complete);
  238. return;
  239. }
  240. if (isr & KW_I2C_IRQ_ADDR) {
  241. ack = kw_read_reg(reg_status);
  242. if (host->state != state_addr) {
  243. WRONG_STATE("KW_I2C_IRQ_ADDR");
  244. kw_i2c_do_stop(host, -EIO);
  245. }
  246. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  247. host->result = -ENXIO;
  248. host->state = state_stop;
  249. DBG_LOW("KW: NAK on address\n");
  250. } else {
  251. if (host->len == 0)
  252. kw_i2c_do_stop(host, 0);
  253. else if (host->rw) {
  254. host->state = state_read;
  255. if (host->len > 1)
  256. kw_write_reg(reg_control,
  257. KW_I2C_CTL_AAK);
  258. } else {
  259. host->state = state_write;
  260. kw_write_reg(reg_data, *(host->data++));
  261. host->len--;
  262. }
  263. }
  264. kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  265. }
  266. if (isr & KW_I2C_IRQ_DATA) {
  267. if (host->state == state_read) {
  268. *(host->data++) = kw_read_reg(reg_data);
  269. host->len--;
  270. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  271. if (host->len == 0)
  272. host->state = state_stop;
  273. else if (host->len == 1)
  274. kw_write_reg(reg_control, 0);
  275. } else if (host->state == state_write) {
  276. ack = kw_read_reg(reg_status);
  277. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  278. DBG_LOW("KW: nack on data write\n");
  279. host->result = -EFBIG;
  280. host->state = state_stop;
  281. } else if (host->len) {
  282. kw_write_reg(reg_data, *(host->data++));
  283. host->len--;
  284. } else
  285. kw_i2c_do_stop(host, 0);
  286. } else {
  287. WRONG_STATE("KW_I2C_IRQ_DATA");
  288. if (host->state != state_stop)
  289. kw_i2c_do_stop(host, -EIO);
  290. }
  291. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  292. }
  293. if (isr & KW_I2C_IRQ_STOP) {
  294. kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);
  295. if (host->state != state_stop) {
  296. WRONG_STATE("KW_I2C_IRQ_STOP");
  297. host->result = -EIO;
  298. }
  299. host->state = state_idle;
  300. if (!host->polled)
  301. complete(&host->complete);
  302. }
  303. /* Below should only happen in manual mode which we don't use ... */
  304. if (isr & KW_I2C_IRQ_START)
  305. kw_write_reg(reg_isr, KW_I2C_IRQ_START);
  306. }
  307. /* Interrupt handler */
  308. static irqreturn_t kw_i2c_irq(int irq, void *dev_id)
  309. {
  310. struct pmac_i2c_host_kw *host = dev_id;
  311. unsigned long flags;
  312. spin_lock_irqsave(&host->lock, flags);
  313. del_timer(&host->timeout_timer);
  314. kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
  315. if (host->state != state_idle) {
  316. host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
  317. add_timer(&host->timeout_timer);
  318. }
  319. spin_unlock_irqrestore(&host->lock, flags);
  320. return IRQ_HANDLED;
  321. }
  322. static void kw_i2c_timeout(unsigned long data)
  323. {
  324. struct pmac_i2c_host_kw *host = (struct pmac_i2c_host_kw *)data;
  325. unsigned long flags;
  326. spin_lock_irqsave(&host->lock, flags);
  327. kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
  328. if (host->state != state_idle) {
  329. host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
  330. add_timer(&host->timeout_timer);
  331. }
  332. spin_unlock_irqrestore(&host->lock, flags);
  333. }
  334. static int kw_i2c_open(struct pmac_i2c_bus *bus)
  335. {
  336. struct pmac_i2c_host_kw *host = bus->hostdata;
  337. mutex_lock(&host->mutex);
  338. return 0;
  339. }
  340. static void kw_i2c_close(struct pmac_i2c_bus *bus)
  341. {
  342. struct pmac_i2c_host_kw *host = bus->hostdata;
  343. mutex_unlock(&host->mutex);
  344. }
  345. static int kw_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  346. u32 subaddr, u8 *data, int len)
  347. {
  348. struct pmac_i2c_host_kw *host = bus->hostdata;
  349. u8 mode_reg = host->speed;
  350. int use_irq = host->irq != NO_IRQ && !bus->polled;
  351. /* Setup mode & subaddress if any */
  352. switch(bus->mode) {
  353. case pmac_i2c_mode_dumb:
  354. return -EINVAL;
  355. case pmac_i2c_mode_std:
  356. mode_reg |= KW_I2C_MODE_STANDARD;
  357. if (subsize != 0)
  358. return -EINVAL;
  359. break;
  360. case pmac_i2c_mode_stdsub:
  361. mode_reg |= KW_I2C_MODE_STANDARDSUB;
  362. if (subsize != 1)
  363. return -EINVAL;
  364. break;
  365. case pmac_i2c_mode_combined:
  366. mode_reg |= KW_I2C_MODE_COMBINED;
  367. if (subsize != 1)
  368. return -EINVAL;
  369. break;
  370. }
  371. /* Setup channel & clear pending irqs */
  372. kw_write_reg(reg_isr, kw_read_reg(reg_isr));
  373. kw_write_reg(reg_mode, mode_reg | (bus->channel << 4));
  374. kw_write_reg(reg_status, 0);
  375. /* Set up address and r/w bit, strip possible stale bus number from
  376. * address top bits
  377. */
  378. kw_write_reg(reg_addr, addrdir & 0xff);
  379. /* Set up the sub address */
  380. if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
  381. || (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
  382. kw_write_reg(reg_subaddr, subaddr);
  383. /* Prepare for async operations */
  384. host->data = data;
  385. host->len = len;
  386. host->state = state_addr;
  387. host->result = 0;
  388. host->rw = (addrdir & 1);
  389. host->polled = bus->polled;
  390. /* Enable interrupt if not using polled mode and interrupt is
  391. * available
  392. */
  393. if (use_irq) {
  394. /* Clear completion */
  395. INIT_COMPLETION(host->complete);
  396. /* Ack stale interrupts */
  397. kw_write_reg(reg_isr, kw_read_reg(reg_isr));
  398. /* Arm timeout */
  399. host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
  400. add_timer(&host->timeout_timer);
  401. /* Enable emission */
  402. kw_write_reg(reg_ier, KW_I2C_IRQ_MASK);
  403. }
  404. /* Start sending address */
  405. kw_write_reg(reg_control, KW_I2C_CTL_XADDR);
  406. /* Wait for completion */
  407. if (use_irq)
  408. wait_for_completion(&host->complete);
  409. else {
  410. while(host->state != state_idle) {
  411. unsigned long flags;
  412. u8 isr = kw_i2c_wait_interrupt(host);
  413. spin_lock_irqsave(&host->lock, flags);
  414. kw_i2c_handle_interrupt(host, isr);
  415. spin_unlock_irqrestore(&host->lock, flags);
  416. }
  417. }
  418. /* Disable emission */
  419. kw_write_reg(reg_ier, 0);
  420. return host->result;
  421. }
  422. static struct pmac_i2c_host_kw *__init kw_i2c_host_init(struct device_node *np)
  423. {
  424. struct pmac_i2c_host_kw *host;
  425. const u32 *psteps, *prate, *addrp;
  426. u32 steps;
  427. host = kzalloc(sizeof(struct pmac_i2c_host_kw), GFP_KERNEL);
  428. if (host == NULL) {
  429. printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
  430. np->full_name);
  431. return NULL;
  432. }
  433. /* Apple is kind enough to provide a valid AAPL,address property
  434. * on all i2c keywest nodes so far ... we would have to fallback
  435. * to macio parsing if that wasn't the case
  436. */
  437. addrp = of_get_property(np, "AAPL,address", NULL);
  438. if (addrp == NULL) {
  439. printk(KERN_ERR "low_i2c: Can't find address for %s\n",
  440. np->full_name);
  441. kfree(host);
  442. return NULL;
  443. }
  444. mutex_init(&host->mutex);
  445. init_completion(&host->complete);
  446. spin_lock_init(&host->lock);
  447. init_timer(&host->timeout_timer);
  448. host->timeout_timer.function = kw_i2c_timeout;
  449. host->timeout_timer.data = (unsigned long)host;
  450. psteps = of_get_property(np, "AAPL,address-step", NULL);
  451. steps = psteps ? (*psteps) : 0x10;
  452. for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
  453. steps >>= 1;
  454. /* Select interface rate */
  455. host->speed = KW_I2C_MODE_25KHZ;
  456. prate = of_get_property(np, "AAPL,i2c-rate", NULL);
  457. if (prate) switch(*prate) {
  458. case 100:
  459. host->speed = KW_I2C_MODE_100KHZ;
  460. break;
  461. case 50:
  462. host->speed = KW_I2C_MODE_50KHZ;
  463. break;
  464. case 25:
  465. host->speed = KW_I2C_MODE_25KHZ;
  466. break;
  467. }
  468. host->irq = irq_of_parse_and_map(np, 0);
  469. if (host->irq == NO_IRQ)
  470. printk(KERN_WARNING
  471. "low_i2c: Failed to map interrupt for %s\n",
  472. np->full_name);
  473. host->base = ioremap((*addrp), 0x1000);
  474. if (host->base == NULL) {
  475. printk(KERN_ERR "low_i2c: Can't map registers for %s\n",
  476. np->full_name);
  477. kfree(host);
  478. return NULL;
  479. }
  480. /* Make sure IRQ is disabled */
  481. kw_write_reg(reg_ier, 0);
  482. /* Request chip interrupt. We set IRQF_TIMER because we don't
  483. * want that interrupt disabled between the 2 passes of driver
  484. * suspend or we'll have issues running the pfuncs
  485. */
  486. if (request_irq(host->irq, kw_i2c_irq, IRQF_TIMER, "keywest i2c", host))
  487. host->irq = NO_IRQ;
  488. printk(KERN_INFO "KeyWest i2c @0x%08x irq %d %s\n",
  489. *addrp, host->irq, np->full_name);
  490. return host;
  491. }
  492. static void __init kw_i2c_add(struct pmac_i2c_host_kw *host,
  493. struct device_node *controller,
  494. struct device_node *busnode,
  495. int channel)
  496. {
  497. struct pmac_i2c_bus *bus;
  498. bus = kzalloc(sizeof(struct pmac_i2c_bus), GFP_KERNEL);
  499. if (bus == NULL)
  500. return;
  501. bus->controller = of_node_get(controller);
  502. bus->busnode = of_node_get(busnode);
  503. bus->type = pmac_i2c_bus_keywest;
  504. bus->hostdata = host;
  505. bus->channel = channel;
  506. bus->mode = pmac_i2c_mode_std;
  507. bus->open = kw_i2c_open;
  508. bus->close = kw_i2c_close;
  509. bus->xfer = kw_i2c_xfer;
  510. mutex_init(&bus->mutex);
  511. if (controller == busnode)
  512. bus->flags = pmac_i2c_multibus;
  513. list_add(&bus->link, &pmac_i2c_busses);
  514. printk(KERN_INFO " channel %d bus %s\n", channel,
  515. (controller == busnode) ? "<multibus>" : busnode->full_name);
  516. }
  517. static void __init kw_i2c_probe(void)
  518. {
  519. struct device_node *np, *child, *parent;
  520. /* Probe keywest-i2c busses */
  521. for_each_compatible_node(np, "i2c","keywest-i2c") {
  522. struct pmac_i2c_host_kw *host;
  523. int multibus, chans, i;
  524. /* Found one, init a host structure */
  525. host = kw_i2c_host_init(np);
  526. if (host == NULL)
  527. continue;
  528. /* Now check if we have a multibus setup (old style) or if we
  529. * have proper bus nodes. Note that the "new" way (proper bus
  530. * nodes) might cause us to not create some busses that are
  531. * kept hidden in the device-tree. In the future, we might
  532. * want to work around that by creating busses without a node
  533. * but not for now
  534. */
  535. child = of_get_next_child(np, NULL);
  536. multibus = !child || strcmp(child->name, "i2c-bus");
  537. of_node_put(child);
  538. /* For a multibus setup, we get the bus count based on the
  539. * parent type
  540. */
  541. if (multibus) {
  542. parent = of_get_parent(np);
  543. if (parent == NULL)
  544. continue;
  545. chans = parent->name[0] == 'u' ? 2 : 1;
  546. for (i = 0; i < chans; i++)
  547. kw_i2c_add(host, np, np, i);
  548. } else {
  549. for (child = NULL;
  550. (child = of_get_next_child(np, child)) != NULL;) {
  551. const u32 *reg = of_get_property(child,
  552. "reg", NULL);
  553. if (reg == NULL)
  554. continue;
  555. kw_i2c_add(host, np, child, *reg);
  556. }
  557. }
  558. }
  559. }
  560. /*
  561. *
  562. * PMU implementation
  563. *
  564. */
  565. #ifdef CONFIG_ADB_PMU
  566. /*
  567. * i2c command block to the PMU
  568. */
  569. struct pmu_i2c_hdr {
  570. u8 bus;
  571. u8 mode;
  572. u8 bus2;
  573. u8 address;
  574. u8 sub_addr;
  575. u8 comb_addr;
  576. u8 count;
  577. u8 data[];
  578. };
  579. static void pmu_i2c_complete(struct adb_request *req)
  580. {
  581. complete(req->arg);
  582. }
  583. static int pmu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  584. u32 subaddr, u8 *data, int len)
  585. {
  586. struct adb_request *req = bus->hostdata;
  587. struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req->data[1];
  588. struct completion comp;
  589. int read = addrdir & 1;
  590. int retry;
  591. int rc = 0;
  592. /* For now, limit ourselves to 16 bytes transfers */
  593. if (len > 16)
  594. return -EINVAL;
  595. init_completion(&comp);
  596. for (retry = 0; retry < 16; retry++) {
  597. memset(req, 0, sizeof(struct adb_request));
  598. hdr->bus = bus->channel;
  599. hdr->count = len;
  600. switch(bus->mode) {
  601. case pmac_i2c_mode_std:
  602. if (subsize != 0)
  603. return -EINVAL;
  604. hdr->address = addrdir;
  605. hdr->mode = PMU_I2C_MODE_SIMPLE;
  606. break;
  607. case pmac_i2c_mode_stdsub:
  608. case pmac_i2c_mode_combined:
  609. if (subsize != 1)
  610. return -EINVAL;
  611. hdr->address = addrdir & 0xfe;
  612. hdr->comb_addr = addrdir;
  613. hdr->sub_addr = subaddr;
  614. if (bus->mode == pmac_i2c_mode_stdsub)
  615. hdr->mode = PMU_I2C_MODE_STDSUB;
  616. else
  617. hdr->mode = PMU_I2C_MODE_COMBINED;
  618. break;
  619. default:
  620. return -EINVAL;
  621. }
  622. INIT_COMPLETION(comp);
  623. req->data[0] = PMU_I2C_CMD;
  624. req->reply[0] = 0xff;
  625. req->nbytes = sizeof(struct pmu_i2c_hdr) + 1;
  626. req->done = pmu_i2c_complete;
  627. req->arg = &comp;
  628. if (!read && len) {
  629. memcpy(hdr->data, data, len);
  630. req->nbytes += len;
  631. }
  632. rc = pmu_queue_request(req);
  633. if (rc)
  634. return rc;
  635. wait_for_completion(&comp);
  636. if (req->reply[0] == PMU_I2C_STATUS_OK)
  637. break;
  638. msleep(15);
  639. }
  640. if (req->reply[0] != PMU_I2C_STATUS_OK)
  641. return -EIO;
  642. for (retry = 0; retry < 16; retry++) {
  643. memset(req, 0, sizeof(struct adb_request));
  644. /* I know that looks like a lot, slow as hell, but darwin
  645. * does it so let's be on the safe side for now
  646. */
  647. msleep(15);
  648. hdr->bus = PMU_I2C_BUS_STATUS;
  649. INIT_COMPLETION(comp);
  650. req->data[0] = PMU_I2C_CMD;
  651. req->reply[0] = 0xff;
  652. req->nbytes = 2;
  653. req->done = pmu_i2c_complete;
  654. req->arg = &comp;
  655. rc = pmu_queue_request(req);
  656. if (rc)
  657. return rc;
  658. wait_for_completion(&comp);
  659. if (req->reply[0] == PMU_I2C_STATUS_OK && !read)
  660. return 0;
  661. if (req->reply[0] == PMU_I2C_STATUS_DATAREAD && read) {
  662. int rlen = req->reply_len - 1;
  663. if (rlen != len) {
  664. printk(KERN_WARNING "low_i2c: PMU returned %d"
  665. " bytes, expected %d !\n", rlen, len);
  666. return -EIO;
  667. }
  668. if (len)
  669. memcpy(data, &req->reply[1], len);
  670. return 0;
  671. }
  672. }
  673. return -EIO;
  674. }
  675. static void __init pmu_i2c_probe(void)
  676. {
  677. struct pmac_i2c_bus *bus;
  678. struct device_node *busnode;
  679. int channel, sz;
  680. if (!pmu_present())
  681. return;
  682. /* There might or might not be a "pmu-i2c" node, we use that
  683. * or via-pmu itself, whatever we find. I haven't seen a machine
  684. * with separate bus nodes, so we assume a multibus setup
  685. */
  686. busnode = of_find_node_by_name(NULL, "pmu-i2c");
  687. if (busnode == NULL)
  688. busnode = of_find_node_by_name(NULL, "via-pmu");
  689. if (busnode == NULL)
  690. return;
  691. printk(KERN_INFO "PMU i2c %s\n", busnode->full_name);
  692. /*
  693. * We add bus 1 and 2 only for now, bus 0 is "special"
  694. */
  695. for (channel = 1; channel <= 2; channel++) {
  696. sz = sizeof(struct pmac_i2c_bus) + sizeof(struct adb_request);
  697. bus = kzalloc(sz, GFP_KERNEL);
  698. if (bus == NULL)
  699. return;
  700. bus->controller = busnode;
  701. bus->busnode = busnode;
  702. bus->type = pmac_i2c_bus_pmu;
  703. bus->channel = channel;
  704. bus->mode = pmac_i2c_mode_std;
  705. bus->hostdata = bus + 1;
  706. bus->xfer = pmu_i2c_xfer;
  707. mutex_init(&bus->mutex);
  708. bus->flags = pmac_i2c_multibus;
  709. list_add(&bus->link, &pmac_i2c_busses);
  710. printk(KERN_INFO " channel %d bus <multibus>\n", channel);
  711. }
  712. }
  713. #endif /* CONFIG_ADB_PMU */
  714. /*
  715. *
  716. * SMU implementation
  717. *
  718. */
  719. #ifdef CONFIG_PMAC_SMU
  720. static void smu_i2c_complete(struct smu_i2c_cmd *cmd, void *misc)
  721. {
  722. complete(misc);
  723. }
  724. static int smu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  725. u32 subaddr, u8 *data, int len)
  726. {
  727. struct smu_i2c_cmd *cmd = bus->hostdata;
  728. struct completion comp;
  729. int read = addrdir & 1;
  730. int rc = 0;
  731. if ((read && len > SMU_I2C_READ_MAX) ||
  732. ((!read) && len > SMU_I2C_WRITE_MAX))
  733. return -EINVAL;
  734. memset(cmd, 0, sizeof(struct smu_i2c_cmd));
  735. cmd->info.bus = bus->channel;
  736. cmd->info.devaddr = addrdir;
  737. cmd->info.datalen = len;
  738. switch(bus->mode) {
  739. case pmac_i2c_mode_std:
  740. if (subsize != 0)
  741. return -EINVAL;
  742. cmd->info.type = SMU_I2C_TRANSFER_SIMPLE;
  743. break;
  744. case pmac_i2c_mode_stdsub:
  745. case pmac_i2c_mode_combined:
  746. if (subsize > 3 || subsize < 1)
  747. return -EINVAL;
  748. cmd->info.sublen = subsize;
  749. /* that's big-endian only but heh ! */
  750. memcpy(&cmd->info.subaddr, ((char *)&subaddr) + (4 - subsize),
  751. subsize);
  752. if (bus->mode == pmac_i2c_mode_stdsub)
  753. cmd->info.type = SMU_I2C_TRANSFER_STDSUB;
  754. else
  755. cmd->info.type = SMU_I2C_TRANSFER_COMBINED;
  756. break;
  757. default:
  758. return -EINVAL;
  759. }
  760. if (!read && len)
  761. memcpy(cmd->info.data, data, len);
  762. init_completion(&comp);
  763. cmd->done = smu_i2c_complete;
  764. cmd->misc = &comp;
  765. rc = smu_queue_i2c(cmd);
  766. if (rc < 0)
  767. return rc;
  768. wait_for_completion(&comp);
  769. rc = cmd->status;
  770. if (read && len)
  771. memcpy(data, cmd->info.data, len);
  772. return rc < 0 ? rc : 0;
  773. }
  774. static void __init smu_i2c_probe(void)
  775. {
  776. struct device_node *controller, *busnode;
  777. struct pmac_i2c_bus *bus;
  778. const u32 *reg;
  779. int sz;
  780. if (!smu_present())
  781. return;
  782. controller = of_find_node_by_name(NULL, "smu-i2c-control");
  783. if (controller == NULL)
  784. controller = of_find_node_by_name(NULL, "smu");
  785. if (controller == NULL)
  786. return;
  787. printk(KERN_INFO "SMU i2c %s\n", controller->full_name);
  788. /* Look for childs, note that they might not be of the right
  789. * type as older device trees mix i2c busses and other thigns
  790. * at the same level
  791. */
  792. for (busnode = NULL;
  793. (busnode = of_get_next_child(controller, busnode)) != NULL;) {
  794. if (strcmp(busnode->type, "i2c") &&
  795. strcmp(busnode->type, "i2c-bus"))
  796. continue;
  797. reg = of_get_property(busnode, "reg", NULL);
  798. if (reg == NULL)
  799. continue;
  800. sz = sizeof(struct pmac_i2c_bus) + sizeof(struct smu_i2c_cmd);
  801. bus = kzalloc(sz, GFP_KERNEL);
  802. if (bus == NULL)
  803. return;
  804. bus->controller = controller;
  805. bus->busnode = of_node_get(busnode);
  806. bus->type = pmac_i2c_bus_smu;
  807. bus->channel = *reg;
  808. bus->mode = pmac_i2c_mode_std;
  809. bus->hostdata = bus + 1;
  810. bus->xfer = smu_i2c_xfer;
  811. mutex_init(&bus->mutex);
  812. bus->flags = 0;
  813. list_add(&bus->link, &pmac_i2c_busses);
  814. printk(KERN_INFO " channel %x bus %s\n",
  815. bus->channel, busnode->full_name);
  816. }
  817. }
  818. #endif /* CONFIG_PMAC_SMU */
  819. /*
  820. *
  821. * Core code
  822. *
  823. */
  824. struct pmac_i2c_bus *pmac_i2c_find_bus(struct device_node *node)
  825. {
  826. struct device_node *p = of_node_get(node);
  827. struct device_node *prev = NULL;
  828. struct pmac_i2c_bus *bus;
  829. while(p) {
  830. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  831. if (p == bus->busnode) {
  832. if (prev && bus->flags & pmac_i2c_multibus) {
  833. const u32 *reg;
  834. reg = of_get_property(prev, "reg",
  835. NULL);
  836. if (!reg)
  837. continue;
  838. if (((*reg) >> 8) != bus->channel)
  839. continue;
  840. }
  841. of_node_put(p);
  842. of_node_put(prev);
  843. return bus;
  844. }
  845. }
  846. of_node_put(prev);
  847. prev = p;
  848. p = of_get_parent(p);
  849. }
  850. return NULL;
  851. }
  852. EXPORT_SYMBOL_GPL(pmac_i2c_find_bus);
  853. u8 pmac_i2c_get_dev_addr(struct device_node *device)
  854. {
  855. const u32 *reg = of_get_property(device, "reg", NULL);
  856. if (reg == NULL)
  857. return 0;
  858. return (*reg) & 0xff;
  859. }
  860. EXPORT_SYMBOL_GPL(pmac_i2c_get_dev_addr);
  861. struct device_node *pmac_i2c_get_controller(struct pmac_i2c_bus *bus)
  862. {
  863. return bus->controller;
  864. }
  865. EXPORT_SYMBOL_GPL(pmac_i2c_get_controller);
  866. struct device_node *pmac_i2c_get_bus_node(struct pmac_i2c_bus *bus)
  867. {
  868. return bus->busnode;
  869. }
  870. EXPORT_SYMBOL_GPL(pmac_i2c_get_bus_node);
  871. int pmac_i2c_get_type(struct pmac_i2c_bus *bus)
  872. {
  873. return bus->type;
  874. }
  875. EXPORT_SYMBOL_GPL(pmac_i2c_get_type);
  876. int pmac_i2c_get_flags(struct pmac_i2c_bus *bus)
  877. {
  878. return bus->flags;
  879. }
  880. EXPORT_SYMBOL_GPL(pmac_i2c_get_flags);
  881. int pmac_i2c_get_channel(struct pmac_i2c_bus *bus)
  882. {
  883. return bus->channel;
  884. }
  885. EXPORT_SYMBOL_GPL(pmac_i2c_get_channel);
  886. struct i2c_adapter *pmac_i2c_get_adapter(struct pmac_i2c_bus *bus)
  887. {
  888. return &bus->adapter;
  889. }
  890. EXPORT_SYMBOL_GPL(pmac_i2c_get_adapter);
  891. struct pmac_i2c_bus *pmac_i2c_adapter_to_bus(struct i2c_adapter *adapter)
  892. {
  893. struct pmac_i2c_bus *bus;
  894. list_for_each_entry(bus, &pmac_i2c_busses, link)
  895. if (&bus->adapter == adapter)
  896. return bus;
  897. return NULL;
  898. }
  899. EXPORT_SYMBOL_GPL(pmac_i2c_adapter_to_bus);
  900. int pmac_i2c_match_adapter(struct device_node *dev, struct i2c_adapter *adapter)
  901. {
  902. struct pmac_i2c_bus *bus = pmac_i2c_find_bus(dev);
  903. if (bus == NULL)
  904. return 0;
  905. return (&bus->adapter == adapter);
  906. }
  907. EXPORT_SYMBOL_GPL(pmac_i2c_match_adapter);
  908. int pmac_low_i2c_lock(struct device_node *np)
  909. {
  910. struct pmac_i2c_bus *bus, *found = NULL;
  911. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  912. if (np == bus->controller) {
  913. found = bus;
  914. break;
  915. }
  916. }
  917. if (!found)
  918. return -ENODEV;
  919. return pmac_i2c_open(bus, 0);
  920. }
  921. EXPORT_SYMBOL_GPL(pmac_low_i2c_lock);
  922. int pmac_low_i2c_unlock(struct device_node *np)
  923. {
  924. struct pmac_i2c_bus *bus, *found = NULL;
  925. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  926. if (np == bus->controller) {
  927. found = bus;
  928. break;
  929. }
  930. }
  931. if (!found)
  932. return -ENODEV;
  933. pmac_i2c_close(bus);
  934. return 0;
  935. }
  936. EXPORT_SYMBOL_GPL(pmac_low_i2c_unlock);
  937. int pmac_i2c_open(struct pmac_i2c_bus *bus, int polled)
  938. {
  939. int rc;
  940. mutex_lock(&bus->mutex);
  941. bus->polled = polled || pmac_i2c_force_poll;
  942. bus->opened = 1;
  943. bus->mode = pmac_i2c_mode_std;
  944. if (bus->open && (rc = bus->open(bus)) != 0) {
  945. bus->opened = 0;
  946. mutex_unlock(&bus->mutex);
  947. return rc;
  948. }
  949. return 0;
  950. }
  951. EXPORT_SYMBOL_GPL(pmac_i2c_open);
  952. void pmac_i2c_close(struct pmac_i2c_bus *bus)
  953. {
  954. WARN_ON(!bus->opened);
  955. if (bus->close)
  956. bus->close(bus);
  957. bus->opened = 0;
  958. mutex_unlock(&bus->mutex);
  959. }
  960. EXPORT_SYMBOL_GPL(pmac_i2c_close);
  961. int pmac_i2c_setmode(struct pmac_i2c_bus *bus, int mode)
  962. {
  963. WARN_ON(!bus->opened);
  964. /* Report me if you see the error below as there might be a new
  965. * "combined4" mode that I need to implement for the SMU bus
  966. */
  967. if (mode < pmac_i2c_mode_dumb || mode > pmac_i2c_mode_combined) {
  968. printk(KERN_ERR "low_i2c: Invalid mode %d requested on"
  969. " bus %s !\n", mode, bus->busnode->full_name);
  970. return -EINVAL;
  971. }
  972. bus->mode = mode;
  973. return 0;
  974. }
  975. EXPORT_SYMBOL_GPL(pmac_i2c_setmode);
  976. int pmac_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  977. u32 subaddr, u8 *data, int len)
  978. {
  979. int rc;
  980. WARN_ON(!bus->opened);
  981. DBG("xfer() chan=%d, addrdir=0x%x, mode=%d, subsize=%d, subaddr=0x%x,"
  982. " %d bytes, bus %s\n", bus->channel, addrdir, bus->mode, subsize,
  983. subaddr, len, bus->busnode->full_name);
  984. rc = bus->xfer(bus, addrdir, subsize, subaddr, data, len);
  985. #ifdef DEBUG
  986. if (rc)
  987. DBG("xfer error %d\n", rc);
  988. #endif
  989. return rc;
  990. }
  991. EXPORT_SYMBOL_GPL(pmac_i2c_xfer);
  992. /* some quirks for platform function decoding */
  993. enum {
  994. pmac_i2c_quirk_invmask = 0x00000001u,
  995. pmac_i2c_quirk_skip = 0x00000002u,
  996. };
  997. static void pmac_i2c_devscan(void (*callback)(struct device_node *dev,
  998. int quirks))
  999. {
  1000. struct pmac_i2c_bus *bus;
  1001. struct device_node *np;
  1002. static struct whitelist_ent {
  1003. char *name;
  1004. char *compatible;
  1005. int quirks;
  1006. } whitelist[] = {
  1007. /* XXX Study device-tree's & apple drivers are get the quirks
  1008. * right !
  1009. */
  1010. /* Workaround: It seems that running the clockspreading
  1011. * properties on the eMac will cause lockups during boot.
  1012. * The machine seems to work fine without that. So for now,
  1013. * let's make sure i2c-hwclock doesn't match about "imic"
  1014. * clocks and we'll figure out if we really need to do
  1015. * something special about those later.
  1016. */
  1017. { "i2c-hwclock", "imic5002", pmac_i2c_quirk_skip },
  1018. { "i2c-hwclock", "imic5003", pmac_i2c_quirk_skip },
  1019. { "i2c-hwclock", NULL, pmac_i2c_quirk_invmask },
  1020. { "i2c-cpu-voltage", NULL, 0},
  1021. { "temp-monitor", NULL, 0 },
  1022. { "supply-monitor", NULL, 0 },
  1023. { NULL, NULL, 0 },
  1024. };
  1025. /* Only some devices need to have platform functions instanciated
  1026. * here. For now, we have a table. Others, like 9554 i2c GPIOs used
  1027. * on Xserve, if we ever do a driver for them, will use their own
  1028. * platform function instance
  1029. */
  1030. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  1031. for (np = NULL;
  1032. (np = of_get_next_child(bus->busnode, np)) != NULL;) {
  1033. struct whitelist_ent *p;
  1034. /* If multibus, check if device is on that bus */
  1035. if (bus->flags & pmac_i2c_multibus)
  1036. if (bus != pmac_i2c_find_bus(np))
  1037. continue;
  1038. for (p = whitelist; p->name != NULL; p++) {
  1039. if (strcmp(np->name, p->name))
  1040. continue;
  1041. if (p->compatible &&
  1042. !of_device_is_compatible(np, p->compatible))
  1043. continue;
  1044. if (p->quirks & pmac_i2c_quirk_skip)
  1045. break;
  1046. callback(np, p->quirks);
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. }
  1052. #define MAX_I2C_DATA 64
  1053. struct pmac_i2c_pf_inst
  1054. {
  1055. struct pmac_i2c_bus *bus;
  1056. u8 addr;
  1057. u8 buffer[MAX_I2C_DATA];
  1058. u8 scratch[MAX_I2C_DATA];
  1059. int bytes;
  1060. int quirks;
  1061. };
  1062. static void* pmac_i2c_do_begin(struct pmf_function *func, struct pmf_args *args)
  1063. {
  1064. struct pmac_i2c_pf_inst *inst;
  1065. struct pmac_i2c_bus *bus;
  1066. bus = pmac_i2c_find_bus(func->node);
  1067. if (bus == NULL) {
  1068. printk(KERN_ERR "low_i2c: Can't find bus for %s (pfunc)\n",
  1069. func->node->full_name);
  1070. return NULL;
  1071. }
  1072. if (pmac_i2c_open(bus, 0)) {
  1073. printk(KERN_ERR "low_i2c: Can't open i2c bus for %s (pfunc)\n",
  1074. func->node->full_name);
  1075. return NULL;
  1076. }
  1077. /* XXX might need GFP_ATOMIC when called during the suspend process,
  1078. * but then, there are already lots of issues with suspending when
  1079. * near OOM that need to be resolved, the allocator itself should
  1080. * probably make GFP_NOIO implicit during suspend
  1081. */
  1082. inst = kzalloc(sizeof(struct pmac_i2c_pf_inst), GFP_KERNEL);
  1083. if (inst == NULL) {
  1084. pmac_i2c_close(bus);
  1085. return NULL;
  1086. }
  1087. inst->bus = bus;
  1088. inst->addr = pmac_i2c_get_dev_addr(func->node);
  1089. inst->quirks = (int)(long)func->driver_data;
  1090. return inst;
  1091. }
  1092. static void pmac_i2c_do_end(struct pmf_function *func, void *instdata)
  1093. {
  1094. struct pmac_i2c_pf_inst *inst = instdata;
  1095. if (inst == NULL)
  1096. return;
  1097. pmac_i2c_close(inst->bus);
  1098. if (inst)
  1099. kfree(inst);
  1100. }
  1101. static int pmac_i2c_do_read(PMF_STD_ARGS, u32 len)
  1102. {
  1103. struct pmac_i2c_pf_inst *inst = instdata;
  1104. inst->bytes = len;
  1105. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 0, 0,
  1106. inst->buffer, len);
  1107. }
  1108. static int pmac_i2c_do_write(PMF_STD_ARGS, u32 len, const u8 *data)
  1109. {
  1110. struct pmac_i2c_pf_inst *inst = instdata;
  1111. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
  1112. (u8 *)data, len);
  1113. }
  1114. /* This function is used to do the masking & OR'ing for the "rmw" type
  1115. * callbacks. Ze should apply the mask and OR in the values in the
  1116. * buffer before writing back. The problem is that it seems that
  1117. * various darwin drivers implement the mask/or differently, thus
  1118. * we need to check the quirks first
  1119. */
  1120. static void pmac_i2c_do_apply_rmw(struct pmac_i2c_pf_inst *inst,
  1121. u32 len, const u8 *mask, const u8 *val)
  1122. {
  1123. int i;
  1124. if (inst->quirks & pmac_i2c_quirk_invmask) {
  1125. for (i = 0; i < len; i ++)
  1126. inst->scratch[i] = (inst->buffer[i] & mask[i]) | val[i];
  1127. } else {
  1128. for (i = 0; i < len; i ++)
  1129. inst->scratch[i] = (inst->buffer[i] & ~mask[i])
  1130. | (val[i] & mask[i]);
  1131. }
  1132. }
  1133. static int pmac_i2c_do_rmw(PMF_STD_ARGS, u32 masklen, u32 valuelen,
  1134. u32 totallen, const u8 *maskdata,
  1135. const u8 *valuedata)
  1136. {
  1137. struct pmac_i2c_pf_inst *inst = instdata;
  1138. if (masklen > inst->bytes || valuelen > inst->bytes ||
  1139. totallen > inst->bytes || valuelen > masklen)
  1140. return -EINVAL;
  1141. pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
  1142. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
  1143. inst->scratch, totallen);
  1144. }
  1145. static int pmac_i2c_do_read_sub(PMF_STD_ARGS, u8 subaddr, u32 len)
  1146. {
  1147. struct pmac_i2c_pf_inst *inst = instdata;
  1148. inst->bytes = len;
  1149. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 1, subaddr,
  1150. inst->buffer, len);
  1151. }
  1152. static int pmac_i2c_do_write_sub(PMF_STD_ARGS, u8 subaddr, u32 len,
  1153. const u8 *data)
  1154. {
  1155. struct pmac_i2c_pf_inst *inst = instdata;
  1156. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
  1157. subaddr, (u8 *)data, len);
  1158. }
  1159. static int pmac_i2c_do_set_mode(PMF_STD_ARGS, int mode)
  1160. {
  1161. struct pmac_i2c_pf_inst *inst = instdata;
  1162. return pmac_i2c_setmode(inst->bus, mode);
  1163. }
  1164. static int pmac_i2c_do_rmw_sub(PMF_STD_ARGS, u8 subaddr, u32 masklen,
  1165. u32 valuelen, u32 totallen, const u8 *maskdata,
  1166. const u8 *valuedata)
  1167. {
  1168. struct pmac_i2c_pf_inst *inst = instdata;
  1169. if (masklen > inst->bytes || valuelen > inst->bytes ||
  1170. totallen > inst->bytes || valuelen > masklen)
  1171. return -EINVAL;
  1172. pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
  1173. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
  1174. subaddr, inst->scratch, totallen);
  1175. }
  1176. static int pmac_i2c_do_mask_and_comp(PMF_STD_ARGS, u32 len,
  1177. const u8 *maskdata,
  1178. const u8 *valuedata)
  1179. {
  1180. struct pmac_i2c_pf_inst *inst = instdata;
  1181. int i, match;
  1182. /* Get return value pointer, it's assumed to be a u32 */
  1183. if (!args || !args->count || !args->u[0].p)
  1184. return -EINVAL;
  1185. /* Check buffer */
  1186. if (len > inst->bytes)
  1187. return -EINVAL;
  1188. for (i = 0, match = 1; match && i < len; i ++)
  1189. if ((inst->buffer[i] & maskdata[i]) != valuedata[i])
  1190. match = 0;
  1191. *args->u[0].p = match;
  1192. return 0;
  1193. }
  1194. static int pmac_i2c_do_delay(PMF_STD_ARGS, u32 duration)
  1195. {
  1196. msleep((duration + 999) / 1000);
  1197. return 0;
  1198. }
  1199. static struct pmf_handlers pmac_i2c_pfunc_handlers = {
  1200. .begin = pmac_i2c_do_begin,
  1201. .end = pmac_i2c_do_end,
  1202. .read_i2c = pmac_i2c_do_read,
  1203. .write_i2c = pmac_i2c_do_write,
  1204. .rmw_i2c = pmac_i2c_do_rmw,
  1205. .read_i2c_sub = pmac_i2c_do_read_sub,
  1206. .write_i2c_sub = pmac_i2c_do_write_sub,
  1207. .rmw_i2c_sub = pmac_i2c_do_rmw_sub,
  1208. .set_i2c_mode = pmac_i2c_do_set_mode,
  1209. .mask_and_compare = pmac_i2c_do_mask_and_comp,
  1210. .delay = pmac_i2c_do_delay,
  1211. };
  1212. static void __init pmac_i2c_dev_create(struct device_node *np, int quirks)
  1213. {
  1214. DBG("dev_create(%s)\n", np->full_name);
  1215. pmf_register_driver(np, &pmac_i2c_pfunc_handlers,
  1216. (void *)(long)quirks);
  1217. }
  1218. static void __init pmac_i2c_dev_init(struct device_node *np, int quirks)
  1219. {
  1220. DBG("dev_create(%s)\n", np->full_name);
  1221. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
  1222. }
  1223. static void pmac_i2c_dev_suspend(struct device_node *np, int quirks)
  1224. {
  1225. DBG("dev_suspend(%s)\n", np->full_name);
  1226. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_SLEEP, NULL);
  1227. }
  1228. static void pmac_i2c_dev_resume(struct device_node *np, int quirks)
  1229. {
  1230. DBG("dev_resume(%s)\n", np->full_name);
  1231. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_WAKE, NULL);
  1232. }
  1233. void pmac_pfunc_i2c_suspend(void)
  1234. {
  1235. pmac_i2c_devscan(pmac_i2c_dev_suspend);
  1236. }
  1237. void pmac_pfunc_i2c_resume(void)
  1238. {
  1239. pmac_i2c_devscan(pmac_i2c_dev_resume);
  1240. }
  1241. /*
  1242. * Initialize us: probe all i2c busses on the machine, instantiate
  1243. * busses and platform functions as needed.
  1244. */
  1245. /* This is non-static as it might be called early by smp code */
  1246. int __init pmac_i2c_init(void)
  1247. {
  1248. static int i2c_inited;
  1249. if (i2c_inited)
  1250. return 0;
  1251. i2c_inited = 1;
  1252. /* Probe keywest-i2c busses */
  1253. kw_i2c_probe();
  1254. #ifdef CONFIG_ADB_PMU
  1255. /* Probe PMU i2c busses */
  1256. pmu_i2c_probe();
  1257. #endif
  1258. #ifdef CONFIG_PMAC_SMU
  1259. /* Probe SMU i2c busses */
  1260. smu_i2c_probe();
  1261. #endif
  1262. /* Now add plaform functions for some known devices */
  1263. pmac_i2c_devscan(pmac_i2c_dev_create);
  1264. return 0;
  1265. }
  1266. machine_arch_initcall(powermac, pmac_i2c_init);
  1267. /* Since pmac_i2c_init can be called too early for the platform device
  1268. * registration, we need to do it at a later time. In our case, subsys
  1269. * happens to fit well, though I agree it's a bit of a hack...
  1270. */
  1271. static int __init pmac_i2c_create_platform_devices(void)
  1272. {
  1273. struct pmac_i2c_bus *bus;
  1274. int i = 0;
  1275. /* In the case where we are initialized from smp_init(), we must
  1276. * not use the timer (and thus the irq). It's safe from now on
  1277. * though
  1278. */
  1279. pmac_i2c_force_poll = 0;
  1280. /* Create platform devices */
  1281. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  1282. bus->platform_dev =
  1283. platform_device_alloc("i2c-powermac", i++);
  1284. if (bus->platform_dev == NULL)
  1285. return -ENOMEM;
  1286. bus->platform_dev->dev.platform_data = bus;
  1287. platform_device_add(bus->platform_dev);
  1288. }
  1289. /* Now call platform "init" functions */
  1290. pmac_i2c_devscan(pmac_i2c_dev_init);
  1291. return 0;
  1292. }
  1293. machine_subsys_initcall(powermac, pmac_i2c_create_platform_devices);