low_i2c.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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/completion.h>
  42. #include <linux/timer.h>
  43. #include <linux/mutex.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 */
  483. if (request_irq(host->irq, kw_i2c_irq, 0, "keywest i2c", host))
  484. host->irq = NO_IRQ;
  485. printk(KERN_INFO "KeyWest i2c @0x%08x irq %d %s\n",
  486. *addrp, host->irq, np->full_name);
  487. return host;
  488. }
  489. static void __init kw_i2c_add(struct pmac_i2c_host_kw *host,
  490. struct device_node *controller,
  491. struct device_node *busnode,
  492. int channel)
  493. {
  494. struct pmac_i2c_bus *bus;
  495. bus = kzalloc(sizeof(struct pmac_i2c_bus), GFP_KERNEL);
  496. if (bus == NULL)
  497. return;
  498. bus->controller = of_node_get(controller);
  499. bus->busnode = of_node_get(busnode);
  500. bus->type = pmac_i2c_bus_keywest;
  501. bus->hostdata = host;
  502. bus->channel = channel;
  503. bus->mode = pmac_i2c_mode_std;
  504. bus->open = kw_i2c_open;
  505. bus->close = kw_i2c_close;
  506. bus->xfer = kw_i2c_xfer;
  507. mutex_init(&bus->mutex);
  508. if (controller == busnode)
  509. bus->flags = pmac_i2c_multibus;
  510. list_add(&bus->link, &pmac_i2c_busses);
  511. printk(KERN_INFO " channel %d bus %s\n", channel,
  512. (controller == busnode) ? "<multibus>" : busnode->full_name);
  513. }
  514. static void __init kw_i2c_probe(void)
  515. {
  516. struct device_node *np, *child, *parent;
  517. /* Probe keywest-i2c busses */
  518. for (np = NULL;
  519. (np = of_find_compatible_node(np, "i2c","keywest-i2c")) != NULL;){
  520. struct pmac_i2c_host_kw *host;
  521. int multibus, chans, i;
  522. /* Found one, init a host structure */
  523. host = kw_i2c_host_init(np);
  524. if (host == NULL)
  525. continue;
  526. /* Now check if we have a multibus setup (old style) or if we
  527. * have proper bus nodes. Note that the "new" way (proper bus
  528. * nodes) might cause us to not create some busses that are
  529. * kept hidden in the device-tree. In the future, we might
  530. * want to work around that by creating busses without a node
  531. * but not for now
  532. */
  533. child = of_get_next_child(np, NULL);
  534. multibus = !child || strcmp(child->name, "i2c-bus");
  535. of_node_put(child);
  536. /* For a multibus setup, we get the bus count based on the
  537. * parent type
  538. */
  539. if (multibus) {
  540. parent = of_get_parent(np);
  541. if (parent == NULL)
  542. continue;
  543. chans = parent->name[0] == 'u' ? 2 : 1;
  544. for (i = 0; i < chans; i++)
  545. kw_i2c_add(host, np, np, i);
  546. } else {
  547. for (child = NULL;
  548. (child = of_get_next_child(np, child)) != NULL;) {
  549. const u32 *reg = of_get_property(child,
  550. "reg", NULL);
  551. if (reg == NULL)
  552. continue;
  553. kw_i2c_add(host, np, child, *reg);
  554. }
  555. }
  556. }
  557. }
  558. /*
  559. *
  560. * PMU implementation
  561. *
  562. */
  563. #ifdef CONFIG_ADB_PMU
  564. /*
  565. * i2c command block to the PMU
  566. */
  567. struct pmu_i2c_hdr {
  568. u8 bus;
  569. u8 mode;
  570. u8 bus2;
  571. u8 address;
  572. u8 sub_addr;
  573. u8 comb_addr;
  574. u8 count;
  575. u8 data[];
  576. };
  577. static void pmu_i2c_complete(struct adb_request *req)
  578. {
  579. complete(req->arg);
  580. }
  581. static int pmu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  582. u32 subaddr, u8 *data, int len)
  583. {
  584. struct adb_request *req = bus->hostdata;
  585. struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req->data[1];
  586. struct completion comp;
  587. int read = addrdir & 1;
  588. int retry;
  589. int rc = 0;
  590. /* For now, limit ourselves to 16 bytes transfers */
  591. if (len > 16)
  592. return -EINVAL;
  593. init_completion(&comp);
  594. for (retry = 0; retry < 16; retry++) {
  595. memset(req, 0, sizeof(struct adb_request));
  596. hdr->bus = bus->channel;
  597. hdr->count = len;
  598. switch(bus->mode) {
  599. case pmac_i2c_mode_std:
  600. if (subsize != 0)
  601. return -EINVAL;
  602. hdr->address = addrdir;
  603. hdr->mode = PMU_I2C_MODE_SIMPLE;
  604. break;
  605. case pmac_i2c_mode_stdsub:
  606. case pmac_i2c_mode_combined:
  607. if (subsize != 1)
  608. return -EINVAL;
  609. hdr->address = addrdir & 0xfe;
  610. hdr->comb_addr = addrdir;
  611. hdr->sub_addr = subaddr;
  612. if (bus->mode == pmac_i2c_mode_stdsub)
  613. hdr->mode = PMU_I2C_MODE_STDSUB;
  614. else
  615. hdr->mode = PMU_I2C_MODE_COMBINED;
  616. break;
  617. default:
  618. return -EINVAL;
  619. }
  620. INIT_COMPLETION(comp);
  621. req->data[0] = PMU_I2C_CMD;
  622. req->reply[0] = 0xff;
  623. req->nbytes = sizeof(struct pmu_i2c_hdr) + 1;
  624. req->done = pmu_i2c_complete;
  625. req->arg = &comp;
  626. if (!read && len) {
  627. memcpy(hdr->data, data, len);
  628. req->nbytes += len;
  629. }
  630. rc = pmu_queue_request(req);
  631. if (rc)
  632. return rc;
  633. wait_for_completion(&comp);
  634. if (req->reply[0] == PMU_I2C_STATUS_OK)
  635. break;
  636. msleep(15);
  637. }
  638. if (req->reply[0] != PMU_I2C_STATUS_OK)
  639. return -EIO;
  640. for (retry = 0; retry < 16; retry++) {
  641. memset(req, 0, sizeof(struct adb_request));
  642. /* I know that looks like a lot, slow as hell, but darwin
  643. * does it so let's be on the safe side for now
  644. */
  645. msleep(15);
  646. hdr->bus = PMU_I2C_BUS_STATUS;
  647. INIT_COMPLETION(comp);
  648. req->data[0] = PMU_I2C_CMD;
  649. req->reply[0] = 0xff;
  650. req->nbytes = 2;
  651. req->done = pmu_i2c_complete;
  652. req->arg = &comp;
  653. rc = pmu_queue_request(req);
  654. if (rc)
  655. return rc;
  656. wait_for_completion(&comp);
  657. if (req->reply[0] == PMU_I2C_STATUS_OK && !read)
  658. return 0;
  659. if (req->reply[0] == PMU_I2C_STATUS_DATAREAD && read) {
  660. int rlen = req->reply_len - 1;
  661. if (rlen != len) {
  662. printk(KERN_WARNING "low_i2c: PMU returned %d"
  663. " bytes, expected %d !\n", rlen, len);
  664. return -EIO;
  665. }
  666. if (len)
  667. memcpy(data, &req->reply[1], len);
  668. return 0;
  669. }
  670. }
  671. return -EIO;
  672. }
  673. static void __init pmu_i2c_probe(void)
  674. {
  675. struct pmac_i2c_bus *bus;
  676. struct device_node *busnode;
  677. int channel, sz;
  678. if (!pmu_present())
  679. return;
  680. /* There might or might not be a "pmu-i2c" node, we use that
  681. * or via-pmu itself, whatever we find. I haven't seen a machine
  682. * with separate bus nodes, so we assume a multibus setup
  683. */
  684. busnode = of_find_node_by_name(NULL, "pmu-i2c");
  685. if (busnode == NULL)
  686. busnode = of_find_node_by_name(NULL, "via-pmu");
  687. if (busnode == NULL)
  688. return;
  689. printk(KERN_INFO "PMU i2c %s\n", busnode->full_name);
  690. /*
  691. * We add bus 1 and 2 only for now, bus 0 is "special"
  692. */
  693. for (channel = 1; channel <= 2; channel++) {
  694. sz = sizeof(struct pmac_i2c_bus) + sizeof(struct adb_request);
  695. bus = kzalloc(sz, GFP_KERNEL);
  696. if (bus == NULL)
  697. return;
  698. bus->controller = busnode;
  699. bus->busnode = busnode;
  700. bus->type = pmac_i2c_bus_pmu;
  701. bus->channel = channel;
  702. bus->mode = pmac_i2c_mode_std;
  703. bus->hostdata = bus + 1;
  704. bus->xfer = pmu_i2c_xfer;
  705. mutex_init(&bus->mutex);
  706. bus->flags = pmac_i2c_multibus;
  707. list_add(&bus->link, &pmac_i2c_busses);
  708. printk(KERN_INFO " channel %d bus <multibus>\n", channel);
  709. }
  710. }
  711. #endif /* CONFIG_ADB_PMU */
  712. /*
  713. *
  714. * SMU implementation
  715. *
  716. */
  717. #ifdef CONFIG_PMAC_SMU
  718. static void smu_i2c_complete(struct smu_i2c_cmd *cmd, void *misc)
  719. {
  720. complete(misc);
  721. }
  722. static int smu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  723. u32 subaddr, u8 *data, int len)
  724. {
  725. struct smu_i2c_cmd *cmd = bus->hostdata;
  726. struct completion comp;
  727. int read = addrdir & 1;
  728. int rc = 0;
  729. if ((read && len > SMU_I2C_READ_MAX) ||
  730. ((!read) && len > SMU_I2C_WRITE_MAX))
  731. return -EINVAL;
  732. memset(cmd, 0, sizeof(struct smu_i2c_cmd));
  733. cmd->info.bus = bus->channel;
  734. cmd->info.devaddr = addrdir;
  735. cmd->info.datalen = len;
  736. switch(bus->mode) {
  737. case pmac_i2c_mode_std:
  738. if (subsize != 0)
  739. return -EINVAL;
  740. cmd->info.type = SMU_I2C_TRANSFER_SIMPLE;
  741. break;
  742. case pmac_i2c_mode_stdsub:
  743. case pmac_i2c_mode_combined:
  744. if (subsize > 3 || subsize < 1)
  745. return -EINVAL;
  746. cmd->info.sublen = subsize;
  747. /* that's big-endian only but heh ! */
  748. memcpy(&cmd->info.subaddr, ((char *)&subaddr) + (4 - subsize),
  749. subsize);
  750. if (bus->mode == pmac_i2c_mode_stdsub)
  751. cmd->info.type = SMU_I2C_TRANSFER_STDSUB;
  752. else
  753. cmd->info.type = SMU_I2C_TRANSFER_COMBINED;
  754. break;
  755. default:
  756. return -EINVAL;
  757. }
  758. if (!read && len)
  759. memcpy(cmd->info.data, data, len);
  760. init_completion(&comp);
  761. cmd->done = smu_i2c_complete;
  762. cmd->misc = &comp;
  763. rc = smu_queue_i2c(cmd);
  764. if (rc < 0)
  765. return rc;
  766. wait_for_completion(&comp);
  767. rc = cmd->status;
  768. if (read && len)
  769. memcpy(data, cmd->info.data, len);
  770. return rc < 0 ? rc : 0;
  771. }
  772. static void __init smu_i2c_probe(void)
  773. {
  774. struct device_node *controller, *busnode;
  775. struct pmac_i2c_bus *bus;
  776. const u32 *reg;
  777. int sz;
  778. if (!smu_present())
  779. return;
  780. controller = of_find_node_by_name(NULL, "smu-i2c-control");
  781. if (controller == NULL)
  782. controller = of_find_node_by_name(NULL, "smu");
  783. if (controller == NULL)
  784. return;
  785. printk(KERN_INFO "SMU i2c %s\n", controller->full_name);
  786. /* Look for childs, note that they might not be of the right
  787. * type as older device trees mix i2c busses and other thigns
  788. * at the same level
  789. */
  790. for (busnode = NULL;
  791. (busnode = of_get_next_child(controller, busnode)) != NULL;) {
  792. if (strcmp(busnode->type, "i2c") &&
  793. strcmp(busnode->type, "i2c-bus"))
  794. continue;
  795. reg = of_get_property(busnode, "reg", NULL);
  796. if (reg == NULL)
  797. continue;
  798. sz = sizeof(struct pmac_i2c_bus) + sizeof(struct smu_i2c_cmd);
  799. bus = kzalloc(sz, GFP_KERNEL);
  800. if (bus == NULL)
  801. return;
  802. bus->controller = controller;
  803. bus->busnode = of_node_get(busnode);
  804. bus->type = pmac_i2c_bus_smu;
  805. bus->channel = *reg;
  806. bus->mode = pmac_i2c_mode_std;
  807. bus->hostdata = bus + 1;
  808. bus->xfer = smu_i2c_xfer;
  809. mutex_init(&bus->mutex);
  810. bus->flags = 0;
  811. list_add(&bus->link, &pmac_i2c_busses);
  812. printk(KERN_INFO " channel %x bus %s\n",
  813. bus->channel, busnode->full_name);
  814. }
  815. }
  816. #endif /* CONFIG_PMAC_SMU */
  817. /*
  818. *
  819. * Core code
  820. *
  821. */
  822. struct pmac_i2c_bus *pmac_i2c_find_bus(struct device_node *node)
  823. {
  824. struct device_node *p = of_node_get(node);
  825. struct device_node *prev = NULL;
  826. struct pmac_i2c_bus *bus;
  827. while(p) {
  828. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  829. if (p == bus->busnode) {
  830. if (prev && bus->flags & pmac_i2c_multibus) {
  831. const u32 *reg;
  832. reg = of_get_property(prev, "reg",
  833. NULL);
  834. if (!reg)
  835. continue;
  836. if (((*reg) >> 8) != bus->channel)
  837. continue;
  838. }
  839. of_node_put(p);
  840. of_node_put(prev);
  841. return bus;
  842. }
  843. }
  844. of_node_put(prev);
  845. prev = p;
  846. p = of_get_parent(p);
  847. }
  848. return NULL;
  849. }
  850. EXPORT_SYMBOL_GPL(pmac_i2c_find_bus);
  851. u8 pmac_i2c_get_dev_addr(struct device_node *device)
  852. {
  853. const u32 *reg = of_get_property(device, "reg", NULL);
  854. if (reg == NULL)
  855. return 0;
  856. return (*reg) & 0xff;
  857. }
  858. EXPORT_SYMBOL_GPL(pmac_i2c_get_dev_addr);
  859. struct device_node *pmac_i2c_get_controller(struct pmac_i2c_bus *bus)
  860. {
  861. return bus->controller;
  862. }
  863. EXPORT_SYMBOL_GPL(pmac_i2c_get_controller);
  864. struct device_node *pmac_i2c_get_bus_node(struct pmac_i2c_bus *bus)
  865. {
  866. return bus->busnode;
  867. }
  868. EXPORT_SYMBOL_GPL(pmac_i2c_get_bus_node);
  869. int pmac_i2c_get_type(struct pmac_i2c_bus *bus)
  870. {
  871. return bus->type;
  872. }
  873. EXPORT_SYMBOL_GPL(pmac_i2c_get_type);
  874. int pmac_i2c_get_flags(struct pmac_i2c_bus *bus)
  875. {
  876. return bus->flags;
  877. }
  878. EXPORT_SYMBOL_GPL(pmac_i2c_get_flags);
  879. int pmac_i2c_get_channel(struct pmac_i2c_bus *bus)
  880. {
  881. return bus->channel;
  882. }
  883. EXPORT_SYMBOL_GPL(pmac_i2c_get_channel);
  884. void pmac_i2c_attach_adapter(struct pmac_i2c_bus *bus,
  885. struct i2c_adapter *adapter)
  886. {
  887. WARN_ON(bus->adapter != NULL);
  888. bus->adapter = adapter;
  889. }
  890. EXPORT_SYMBOL_GPL(pmac_i2c_attach_adapter);
  891. void pmac_i2c_detach_adapter(struct pmac_i2c_bus *bus,
  892. struct i2c_adapter *adapter)
  893. {
  894. WARN_ON(bus->adapter != adapter);
  895. bus->adapter = NULL;
  896. }
  897. EXPORT_SYMBOL_GPL(pmac_i2c_detach_adapter);
  898. struct i2c_adapter *pmac_i2c_get_adapter(struct pmac_i2c_bus *bus)
  899. {
  900. return bus->adapter;
  901. }
  902. EXPORT_SYMBOL_GPL(pmac_i2c_get_adapter);
  903. struct pmac_i2c_bus *pmac_i2c_adapter_to_bus(struct i2c_adapter *adapter)
  904. {
  905. struct pmac_i2c_bus *bus;
  906. list_for_each_entry(bus, &pmac_i2c_busses, link)
  907. if (bus->adapter == adapter)
  908. return bus;
  909. return NULL;
  910. }
  911. EXPORT_SYMBOL_GPL(pmac_i2c_adapter_to_bus);
  912. int pmac_i2c_match_adapter(struct device_node *dev, struct i2c_adapter *adapter)
  913. {
  914. struct pmac_i2c_bus *bus = pmac_i2c_find_bus(dev);
  915. if (bus == NULL)
  916. return 0;
  917. return (bus->adapter == adapter);
  918. }
  919. EXPORT_SYMBOL_GPL(pmac_i2c_match_adapter);
  920. int pmac_low_i2c_lock(struct device_node *np)
  921. {
  922. struct pmac_i2c_bus *bus, *found = NULL;
  923. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  924. if (np == bus->controller) {
  925. found = bus;
  926. break;
  927. }
  928. }
  929. if (!found)
  930. return -ENODEV;
  931. return pmac_i2c_open(bus, 0);
  932. }
  933. EXPORT_SYMBOL_GPL(pmac_low_i2c_lock);
  934. int pmac_low_i2c_unlock(struct device_node *np)
  935. {
  936. struct pmac_i2c_bus *bus, *found = NULL;
  937. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  938. if (np == bus->controller) {
  939. found = bus;
  940. break;
  941. }
  942. }
  943. if (!found)
  944. return -ENODEV;
  945. pmac_i2c_close(bus);
  946. return 0;
  947. }
  948. EXPORT_SYMBOL_GPL(pmac_low_i2c_unlock);
  949. int pmac_i2c_open(struct pmac_i2c_bus *bus, int polled)
  950. {
  951. int rc;
  952. mutex_lock(&bus->mutex);
  953. bus->polled = polled || pmac_i2c_force_poll;
  954. bus->opened = 1;
  955. bus->mode = pmac_i2c_mode_std;
  956. if (bus->open && (rc = bus->open(bus)) != 0) {
  957. bus->opened = 0;
  958. mutex_unlock(&bus->mutex);
  959. return rc;
  960. }
  961. return 0;
  962. }
  963. EXPORT_SYMBOL_GPL(pmac_i2c_open);
  964. void pmac_i2c_close(struct pmac_i2c_bus *bus)
  965. {
  966. WARN_ON(!bus->opened);
  967. if (bus->close)
  968. bus->close(bus);
  969. bus->opened = 0;
  970. mutex_unlock(&bus->mutex);
  971. }
  972. EXPORT_SYMBOL_GPL(pmac_i2c_close);
  973. int pmac_i2c_setmode(struct pmac_i2c_bus *bus, int mode)
  974. {
  975. WARN_ON(!bus->opened);
  976. /* Report me if you see the error below as there might be a new
  977. * "combined4" mode that I need to implement for the SMU bus
  978. */
  979. if (mode < pmac_i2c_mode_dumb || mode > pmac_i2c_mode_combined) {
  980. printk(KERN_ERR "low_i2c: Invalid mode %d requested on"
  981. " bus %s !\n", mode, bus->busnode->full_name);
  982. return -EINVAL;
  983. }
  984. bus->mode = mode;
  985. return 0;
  986. }
  987. EXPORT_SYMBOL_GPL(pmac_i2c_setmode);
  988. int pmac_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  989. u32 subaddr, u8 *data, int len)
  990. {
  991. int rc;
  992. WARN_ON(!bus->opened);
  993. DBG("xfer() chan=%d, addrdir=0x%x, mode=%d, subsize=%d, subaddr=0x%x,"
  994. " %d bytes, bus %s\n", bus->channel, addrdir, bus->mode, subsize,
  995. subaddr, len, bus->busnode->full_name);
  996. rc = bus->xfer(bus, addrdir, subsize, subaddr, data, len);
  997. #ifdef DEBUG
  998. if (rc)
  999. DBG("xfer error %d\n", rc);
  1000. #endif
  1001. return rc;
  1002. }
  1003. EXPORT_SYMBOL_GPL(pmac_i2c_xfer);
  1004. /* some quirks for platform function decoding */
  1005. enum {
  1006. pmac_i2c_quirk_invmask = 0x00000001u,
  1007. pmac_i2c_quirk_skip = 0x00000002u,
  1008. };
  1009. static void pmac_i2c_devscan(void (*callback)(struct device_node *dev,
  1010. int quirks))
  1011. {
  1012. struct pmac_i2c_bus *bus;
  1013. struct device_node *np;
  1014. static struct whitelist_ent {
  1015. char *name;
  1016. char *compatible;
  1017. int quirks;
  1018. } whitelist[] = {
  1019. /* XXX Study device-tree's & apple drivers are get the quirks
  1020. * right !
  1021. */
  1022. /* Workaround: It seems that running the clockspreading
  1023. * properties on the eMac will cause lockups during boot.
  1024. * The machine seems to work fine without that. So for now,
  1025. * let's make sure i2c-hwclock doesn't match about "imic"
  1026. * clocks and we'll figure out if we really need to do
  1027. * something special about those later.
  1028. */
  1029. { "i2c-hwclock", "imic5002", pmac_i2c_quirk_skip },
  1030. { "i2c-hwclock", "imic5003", pmac_i2c_quirk_skip },
  1031. { "i2c-hwclock", NULL, pmac_i2c_quirk_invmask },
  1032. { "i2c-cpu-voltage", NULL, 0},
  1033. { "temp-monitor", NULL, 0 },
  1034. { "supply-monitor", NULL, 0 },
  1035. { NULL, NULL, 0 },
  1036. };
  1037. /* Only some devices need to have platform functions instanciated
  1038. * here. For now, we have a table. Others, like 9554 i2c GPIOs used
  1039. * on Xserve, if we ever do a driver for them, will use their own
  1040. * platform function instance
  1041. */
  1042. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  1043. for (np = NULL;
  1044. (np = of_get_next_child(bus->busnode, np)) != NULL;) {
  1045. struct whitelist_ent *p;
  1046. /* If multibus, check if device is on that bus */
  1047. if (bus->flags & pmac_i2c_multibus)
  1048. if (bus != pmac_i2c_find_bus(np))
  1049. continue;
  1050. for (p = whitelist; p->name != NULL; p++) {
  1051. if (strcmp(np->name, p->name))
  1052. continue;
  1053. if (p->compatible &&
  1054. !of_device_is_compatible(np, p->compatible))
  1055. continue;
  1056. if (p->quirks & pmac_i2c_quirk_skip)
  1057. break;
  1058. callback(np, p->quirks);
  1059. break;
  1060. }
  1061. }
  1062. }
  1063. }
  1064. #define MAX_I2C_DATA 64
  1065. struct pmac_i2c_pf_inst
  1066. {
  1067. struct pmac_i2c_bus *bus;
  1068. u8 addr;
  1069. u8 buffer[MAX_I2C_DATA];
  1070. u8 scratch[MAX_I2C_DATA];
  1071. int bytes;
  1072. int quirks;
  1073. };
  1074. static void* pmac_i2c_do_begin(struct pmf_function *func, struct pmf_args *args)
  1075. {
  1076. struct pmac_i2c_pf_inst *inst;
  1077. struct pmac_i2c_bus *bus;
  1078. bus = pmac_i2c_find_bus(func->node);
  1079. if (bus == NULL) {
  1080. printk(KERN_ERR "low_i2c: Can't find bus for %s (pfunc)\n",
  1081. func->node->full_name);
  1082. return NULL;
  1083. }
  1084. if (pmac_i2c_open(bus, 0)) {
  1085. printk(KERN_ERR "low_i2c: Can't open i2c bus for %s (pfunc)\n",
  1086. func->node->full_name);
  1087. return NULL;
  1088. }
  1089. /* XXX might need GFP_ATOMIC when called during the suspend process,
  1090. * but then, there are already lots of issues with suspending when
  1091. * near OOM that need to be resolved, the allocator itself should
  1092. * probably make GFP_NOIO implicit during suspend
  1093. */
  1094. inst = kzalloc(sizeof(struct pmac_i2c_pf_inst), GFP_KERNEL);
  1095. if (inst == NULL) {
  1096. pmac_i2c_close(bus);
  1097. return NULL;
  1098. }
  1099. inst->bus = bus;
  1100. inst->addr = pmac_i2c_get_dev_addr(func->node);
  1101. inst->quirks = (int)(long)func->driver_data;
  1102. return inst;
  1103. }
  1104. static void pmac_i2c_do_end(struct pmf_function *func, void *instdata)
  1105. {
  1106. struct pmac_i2c_pf_inst *inst = instdata;
  1107. if (inst == NULL)
  1108. return;
  1109. pmac_i2c_close(inst->bus);
  1110. if (inst)
  1111. kfree(inst);
  1112. }
  1113. static int pmac_i2c_do_read(PMF_STD_ARGS, u32 len)
  1114. {
  1115. struct pmac_i2c_pf_inst *inst = instdata;
  1116. inst->bytes = len;
  1117. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 0, 0,
  1118. inst->buffer, len);
  1119. }
  1120. static int pmac_i2c_do_write(PMF_STD_ARGS, u32 len, const u8 *data)
  1121. {
  1122. struct pmac_i2c_pf_inst *inst = instdata;
  1123. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
  1124. (u8 *)data, len);
  1125. }
  1126. /* This function is used to do the masking & OR'ing for the "rmw" type
  1127. * callbacks. Ze should apply the mask and OR in the values in the
  1128. * buffer before writing back. The problem is that it seems that
  1129. * various darwin drivers implement the mask/or differently, thus
  1130. * we need to check the quirks first
  1131. */
  1132. static void pmac_i2c_do_apply_rmw(struct pmac_i2c_pf_inst *inst,
  1133. u32 len, const u8 *mask, const u8 *val)
  1134. {
  1135. int i;
  1136. if (inst->quirks & pmac_i2c_quirk_invmask) {
  1137. for (i = 0; i < len; i ++)
  1138. inst->scratch[i] = (inst->buffer[i] & mask[i]) | val[i];
  1139. } else {
  1140. for (i = 0; i < len; i ++)
  1141. inst->scratch[i] = (inst->buffer[i] & ~mask[i])
  1142. | (val[i] & mask[i]);
  1143. }
  1144. }
  1145. static int pmac_i2c_do_rmw(PMF_STD_ARGS, u32 masklen, u32 valuelen,
  1146. u32 totallen, const u8 *maskdata,
  1147. const u8 *valuedata)
  1148. {
  1149. struct pmac_i2c_pf_inst *inst = instdata;
  1150. if (masklen > inst->bytes || valuelen > inst->bytes ||
  1151. totallen > inst->bytes || valuelen > masklen)
  1152. return -EINVAL;
  1153. pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
  1154. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
  1155. inst->scratch, totallen);
  1156. }
  1157. static int pmac_i2c_do_read_sub(PMF_STD_ARGS, u8 subaddr, u32 len)
  1158. {
  1159. struct pmac_i2c_pf_inst *inst = instdata;
  1160. inst->bytes = len;
  1161. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 1, subaddr,
  1162. inst->buffer, len);
  1163. }
  1164. static int pmac_i2c_do_write_sub(PMF_STD_ARGS, u8 subaddr, u32 len,
  1165. const u8 *data)
  1166. {
  1167. struct pmac_i2c_pf_inst *inst = instdata;
  1168. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
  1169. subaddr, (u8 *)data, len);
  1170. }
  1171. static int pmac_i2c_do_set_mode(PMF_STD_ARGS, int mode)
  1172. {
  1173. struct pmac_i2c_pf_inst *inst = instdata;
  1174. return pmac_i2c_setmode(inst->bus, mode);
  1175. }
  1176. static int pmac_i2c_do_rmw_sub(PMF_STD_ARGS, u8 subaddr, u32 masklen,
  1177. u32 valuelen, u32 totallen, const u8 *maskdata,
  1178. const u8 *valuedata)
  1179. {
  1180. struct pmac_i2c_pf_inst *inst = instdata;
  1181. if (masklen > inst->bytes || valuelen > inst->bytes ||
  1182. totallen > inst->bytes || valuelen > masklen)
  1183. return -EINVAL;
  1184. pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
  1185. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
  1186. subaddr, inst->scratch, totallen);
  1187. }
  1188. static int pmac_i2c_do_mask_and_comp(PMF_STD_ARGS, u32 len,
  1189. const u8 *maskdata,
  1190. const u8 *valuedata)
  1191. {
  1192. struct pmac_i2c_pf_inst *inst = instdata;
  1193. int i, match;
  1194. /* Get return value pointer, it's assumed to be a u32 */
  1195. if (!args || !args->count || !args->u[0].p)
  1196. return -EINVAL;
  1197. /* Check buffer */
  1198. if (len > inst->bytes)
  1199. return -EINVAL;
  1200. for (i = 0, match = 1; match && i < len; i ++)
  1201. if ((inst->buffer[i] & maskdata[i]) != valuedata[i])
  1202. match = 0;
  1203. *args->u[0].p = match;
  1204. return 0;
  1205. }
  1206. static int pmac_i2c_do_delay(PMF_STD_ARGS, u32 duration)
  1207. {
  1208. msleep((duration + 999) / 1000);
  1209. return 0;
  1210. }
  1211. static struct pmf_handlers pmac_i2c_pfunc_handlers = {
  1212. .begin = pmac_i2c_do_begin,
  1213. .end = pmac_i2c_do_end,
  1214. .read_i2c = pmac_i2c_do_read,
  1215. .write_i2c = pmac_i2c_do_write,
  1216. .rmw_i2c = pmac_i2c_do_rmw,
  1217. .read_i2c_sub = pmac_i2c_do_read_sub,
  1218. .write_i2c_sub = pmac_i2c_do_write_sub,
  1219. .rmw_i2c_sub = pmac_i2c_do_rmw_sub,
  1220. .set_i2c_mode = pmac_i2c_do_set_mode,
  1221. .mask_and_compare = pmac_i2c_do_mask_and_comp,
  1222. .delay = pmac_i2c_do_delay,
  1223. };
  1224. static void __init pmac_i2c_dev_create(struct device_node *np, int quirks)
  1225. {
  1226. DBG("dev_create(%s)\n", np->full_name);
  1227. pmf_register_driver(np, &pmac_i2c_pfunc_handlers,
  1228. (void *)(long)quirks);
  1229. }
  1230. static void __init pmac_i2c_dev_init(struct device_node *np, int quirks)
  1231. {
  1232. DBG("dev_create(%s)\n", np->full_name);
  1233. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
  1234. }
  1235. static void pmac_i2c_dev_suspend(struct device_node *np, int quirks)
  1236. {
  1237. DBG("dev_suspend(%s)\n", np->full_name);
  1238. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_SLEEP, NULL);
  1239. }
  1240. static void pmac_i2c_dev_resume(struct device_node *np, int quirks)
  1241. {
  1242. DBG("dev_resume(%s)\n", np->full_name);
  1243. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_WAKE, NULL);
  1244. }
  1245. void pmac_pfunc_i2c_suspend(void)
  1246. {
  1247. pmac_i2c_devscan(pmac_i2c_dev_suspend);
  1248. }
  1249. void pmac_pfunc_i2c_resume(void)
  1250. {
  1251. pmac_i2c_devscan(pmac_i2c_dev_resume);
  1252. }
  1253. /*
  1254. * Initialize us: probe all i2c busses on the machine, instantiate
  1255. * busses and platform functions as needed.
  1256. */
  1257. /* This is non-static as it might be called early by smp code */
  1258. int __init pmac_i2c_init(void)
  1259. {
  1260. static int i2c_inited;
  1261. if (i2c_inited)
  1262. return 0;
  1263. i2c_inited = 1;
  1264. if (!machine_is(powermac))
  1265. return 0;
  1266. /* Probe keywest-i2c busses */
  1267. kw_i2c_probe();
  1268. #ifdef CONFIG_ADB_PMU
  1269. /* Probe PMU i2c busses */
  1270. pmu_i2c_probe();
  1271. #endif
  1272. #ifdef CONFIG_PMAC_SMU
  1273. /* Probe SMU i2c busses */
  1274. smu_i2c_probe();
  1275. #endif
  1276. /* Now add plaform functions for some known devices */
  1277. pmac_i2c_devscan(pmac_i2c_dev_create);
  1278. return 0;
  1279. }
  1280. arch_initcall(pmac_i2c_init);
  1281. /* Since pmac_i2c_init can be called too early for the platform device
  1282. * registration, we need to do it at a later time. In our case, subsys
  1283. * happens to fit well, though I agree it's a bit of a hack...
  1284. */
  1285. static int __init pmac_i2c_create_platform_devices(void)
  1286. {
  1287. struct pmac_i2c_bus *bus;
  1288. int i = 0;
  1289. /* In the case where we are initialized from smp_init(), we must
  1290. * not use the timer (and thus the irq). It's safe from now on
  1291. * though
  1292. */
  1293. pmac_i2c_force_poll = 0;
  1294. /* Create platform devices */
  1295. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  1296. bus->platform_dev =
  1297. platform_device_alloc("i2c-powermac", i++);
  1298. if (bus->platform_dev == NULL)
  1299. return -ENOMEM;
  1300. bus->platform_dev->dev.platform_data = bus;
  1301. platform_device_add(bus->platform_dev);
  1302. }
  1303. /* Now call platform "init" functions */
  1304. pmac_i2c_devscan(pmac_i2c_dev_init);
  1305. return 0;
  1306. }
  1307. subsys_initcall(pmac_i2c_create_platform_devices);