i2c-core.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /* i2c-core.c - a device driver for the iic-bus interface */
  2. /* ------------------------------------------------------------------------- */
  3. /* Copyright (C) 1995-99 Simon G. Vogl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /* ------------------------------------------------------------------------- */
  16. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
  17. All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
  18. SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/idr.h>
  26. #include <linux/seq_file.h>
  27. #include <asm/uaccess.h>
  28. static LIST_HEAD(adapters);
  29. static LIST_HEAD(drivers);
  30. static DECLARE_MUTEX(core_lists);
  31. static DEFINE_IDR(i2c_adapter_idr);
  32. /* match always succeeds, as we want the probe() to tell if we really accept this match */
  33. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  34. {
  35. return 1;
  36. }
  37. static int i2c_bus_suspend(struct device * dev, pm_message_t state)
  38. {
  39. int rc = 0;
  40. if (dev->driver && dev->driver->suspend)
  41. rc = dev->driver->suspend(dev,state,0);
  42. return rc;
  43. }
  44. static int i2c_bus_resume(struct device * dev)
  45. {
  46. int rc = 0;
  47. if (dev->driver && dev->driver->resume)
  48. rc = dev->driver->resume(dev,0);
  49. return rc;
  50. }
  51. struct bus_type i2c_bus_type = {
  52. .name = "i2c",
  53. .match = i2c_device_match,
  54. .suspend = i2c_bus_suspend,
  55. .resume = i2c_bus_resume,
  56. };
  57. static int i2c_device_probe(struct device *dev)
  58. {
  59. return -ENODEV;
  60. }
  61. static int i2c_device_remove(struct device *dev)
  62. {
  63. return 0;
  64. }
  65. void i2c_adapter_dev_release(struct device *dev)
  66. {
  67. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  68. complete(&adap->dev_released);
  69. }
  70. struct device_driver i2c_adapter_driver = {
  71. .name = "i2c_adapter",
  72. .bus = &i2c_bus_type,
  73. .probe = i2c_device_probe,
  74. .remove = i2c_device_remove,
  75. };
  76. static void i2c_adapter_class_dev_release(struct class_device *dev)
  77. {
  78. struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
  79. complete(&adap->class_dev_released);
  80. }
  81. struct class i2c_adapter_class = {
  82. .name = "i2c-adapter",
  83. .release = &i2c_adapter_class_dev_release,
  84. };
  85. static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
  86. {
  87. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  88. return sprintf(buf, "%s\n", adap->name);
  89. }
  90. static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
  91. static void i2c_client_release(struct device *dev)
  92. {
  93. struct i2c_client *client = to_i2c_client(dev);
  94. complete(&client->released);
  95. }
  96. static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. struct i2c_client *client = to_i2c_client(dev);
  99. return sprintf(buf, "%s\n", client->name);
  100. }
  101. /*
  102. * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
  103. * different type of a device. So beware if the DEVICE_ATTR() macro ever
  104. * changes, this definition will also have to change.
  105. */
  106. static struct device_attribute dev_attr_client_name = {
  107. .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
  108. .show = &show_client_name,
  109. };
  110. /* ---------------------------------------------------
  111. * registering functions
  112. * ---------------------------------------------------
  113. */
  114. /* -----
  115. * i2c_add_adapter is called from within the algorithm layer,
  116. * when a new hw adapter registers. A new device is register to be
  117. * available for clients.
  118. */
  119. int i2c_add_adapter(struct i2c_adapter *adap)
  120. {
  121. int id, res = 0;
  122. struct list_head *item;
  123. struct i2c_driver *driver;
  124. down(&core_lists);
  125. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
  126. res = -ENOMEM;
  127. goto out_unlock;
  128. }
  129. res = idr_get_new(&i2c_adapter_idr, adap, &id);
  130. if (res < 0) {
  131. if (res == -EAGAIN)
  132. res = -ENOMEM;
  133. goto out_unlock;
  134. }
  135. adap->nr = id & MAX_ID_MASK;
  136. init_MUTEX(&adap->bus_lock);
  137. init_MUTEX(&adap->clist_lock);
  138. list_add_tail(&adap->list,&adapters);
  139. INIT_LIST_HEAD(&adap->clients);
  140. /* Add the adapter to the driver core.
  141. * If the parent pointer is not set up,
  142. * we add this adapter to the host bus.
  143. */
  144. if (adap->dev.parent == NULL)
  145. adap->dev.parent = &platform_bus;
  146. sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
  147. adap->dev.driver = &i2c_adapter_driver;
  148. adap->dev.release = &i2c_adapter_dev_release;
  149. device_register(&adap->dev);
  150. device_create_file(&adap->dev, &dev_attr_name);
  151. /* Add this adapter to the i2c_adapter class */
  152. memset(&adap->class_dev, 0x00, sizeof(struct class_device));
  153. adap->class_dev.dev = &adap->dev;
  154. adap->class_dev.class = &i2c_adapter_class;
  155. strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
  156. class_device_register(&adap->class_dev);
  157. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  158. /* inform drivers of new adapters */
  159. list_for_each(item,&drivers) {
  160. driver = list_entry(item, struct i2c_driver, list);
  161. if (driver->flags & I2C_DF_NOTIFY)
  162. /* We ignore the return code; if it fails, too bad */
  163. driver->attach_adapter(adap);
  164. }
  165. out_unlock:
  166. up(&core_lists);
  167. return res;
  168. }
  169. int i2c_del_adapter(struct i2c_adapter *adap)
  170. {
  171. struct list_head *item, *_n;
  172. struct i2c_adapter *adap_from_list;
  173. struct i2c_driver *driver;
  174. struct i2c_client *client;
  175. int res = 0;
  176. down(&core_lists);
  177. /* First make sure that this adapter was ever added */
  178. list_for_each_entry(adap_from_list, &adapters, list) {
  179. if (adap_from_list == adap)
  180. break;
  181. }
  182. if (adap_from_list != adap) {
  183. pr_debug("i2c-core: attempting to delete unregistered "
  184. "adapter [%s]\n", adap->name);
  185. res = -EINVAL;
  186. goto out_unlock;
  187. }
  188. list_for_each(item,&drivers) {
  189. driver = list_entry(item, struct i2c_driver, list);
  190. if (driver->detach_adapter)
  191. if ((res = driver->detach_adapter(adap))) {
  192. dev_err(&adap->dev, "detach_adapter failed "
  193. "for driver [%s]\n", driver->name);
  194. goto out_unlock;
  195. }
  196. }
  197. /* detach any active clients. This must be done first, because
  198. * it can fail; in which case we give up. */
  199. list_for_each_safe(item, _n, &adap->clients) {
  200. client = list_entry(item, struct i2c_client, list);
  201. /* detaching devices is unconditional of the set notify
  202. * flag, as _all_ clients that reside on the adapter
  203. * must be deleted, as this would cause invalid states.
  204. */
  205. if ((res=client->driver->detach_client(client))) {
  206. dev_err(&adap->dev, "detach_client failed for client "
  207. "[%s] at address 0x%02x\n", client->name,
  208. client->addr);
  209. goto out_unlock;
  210. }
  211. }
  212. /* clean up the sysfs representation */
  213. init_completion(&adap->dev_released);
  214. init_completion(&adap->class_dev_released);
  215. class_device_unregister(&adap->class_dev);
  216. device_remove_file(&adap->dev, &dev_attr_name);
  217. device_unregister(&adap->dev);
  218. list_del(&adap->list);
  219. /* wait for sysfs to drop all references */
  220. wait_for_completion(&adap->dev_released);
  221. wait_for_completion(&adap->class_dev_released);
  222. /* free dynamically allocated bus id */
  223. idr_remove(&i2c_adapter_idr, adap->nr);
  224. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  225. out_unlock:
  226. up(&core_lists);
  227. return res;
  228. }
  229. /* -----
  230. * What follows is the "upwards" interface: commands for talking to clients,
  231. * which implement the functions to access the physical information of the
  232. * chips.
  233. */
  234. int i2c_add_driver(struct i2c_driver *driver)
  235. {
  236. struct list_head *item;
  237. struct i2c_adapter *adapter;
  238. int res = 0;
  239. down(&core_lists);
  240. /* add the driver to the list of i2c drivers in the driver core */
  241. driver->driver.name = driver->name;
  242. driver->driver.bus = &i2c_bus_type;
  243. driver->driver.probe = i2c_device_probe;
  244. driver->driver.remove = i2c_device_remove;
  245. res = driver_register(&driver->driver);
  246. if (res)
  247. goto out_unlock;
  248. list_add_tail(&driver->list,&drivers);
  249. pr_debug("i2c-core: driver [%s] registered\n", driver->name);
  250. /* now look for instances of driver on our adapters */
  251. if (driver->flags & I2C_DF_NOTIFY) {
  252. list_for_each(item,&adapters) {
  253. adapter = list_entry(item, struct i2c_adapter, list);
  254. driver->attach_adapter(adapter);
  255. }
  256. }
  257. out_unlock:
  258. up(&core_lists);
  259. return res;
  260. }
  261. int i2c_del_driver(struct i2c_driver *driver)
  262. {
  263. struct list_head *item1, *item2, *_n;
  264. struct i2c_client *client;
  265. struct i2c_adapter *adap;
  266. int res = 0;
  267. down(&core_lists);
  268. /* Have a look at each adapter, if clients of this driver are still
  269. * attached. If so, detach them to be able to kill the driver
  270. * afterwards.
  271. *
  272. * Removing clients does not depend on the notify flag, else
  273. * invalid operation might (will!) result, when using stale client
  274. * pointers.
  275. */
  276. list_for_each(item1,&adapters) {
  277. adap = list_entry(item1, struct i2c_adapter, list);
  278. if (driver->detach_adapter) {
  279. if ((res = driver->detach_adapter(adap))) {
  280. dev_err(&adap->dev, "detach_adapter failed "
  281. "for driver [%s]\n", driver->name);
  282. goto out_unlock;
  283. }
  284. } else {
  285. list_for_each_safe(item2, _n, &adap->clients) {
  286. client = list_entry(item2, struct i2c_client, list);
  287. if (client->driver != driver)
  288. continue;
  289. dev_dbg(&adap->dev, "detaching client [%s] "
  290. "at 0x%02x\n", client->name,
  291. client->addr);
  292. if ((res = driver->detach_client(client))) {
  293. dev_err(&adap->dev, "detach_client "
  294. "failed for client [%s] at "
  295. "0x%02x\n", client->name,
  296. client->addr);
  297. goto out_unlock;
  298. }
  299. }
  300. }
  301. }
  302. driver_unregister(&driver->driver);
  303. list_del(&driver->list);
  304. pr_debug("i2c-core: driver [%s] unregistered\n", driver->name);
  305. out_unlock:
  306. up(&core_lists);
  307. return 0;
  308. }
  309. static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  310. {
  311. struct list_head *item;
  312. struct i2c_client *client;
  313. list_for_each(item,&adapter->clients) {
  314. client = list_entry(item, struct i2c_client, list);
  315. if (client->addr == addr)
  316. return -EBUSY;
  317. }
  318. return 0;
  319. }
  320. int i2c_check_addr(struct i2c_adapter *adapter, int addr)
  321. {
  322. int rval;
  323. down(&adapter->clist_lock);
  324. rval = __i2c_check_addr(adapter, addr);
  325. up(&adapter->clist_lock);
  326. return rval;
  327. }
  328. int i2c_attach_client(struct i2c_client *client)
  329. {
  330. struct i2c_adapter *adapter = client->adapter;
  331. down(&adapter->clist_lock);
  332. if (__i2c_check_addr(client->adapter, client->addr)) {
  333. up(&adapter->clist_lock);
  334. return -EBUSY;
  335. }
  336. list_add_tail(&client->list,&adapter->clients);
  337. up(&adapter->clist_lock);
  338. if (adapter->client_register) {
  339. if (adapter->client_register(client)) {
  340. dev_dbg(&adapter->dev, "client_register "
  341. "failed for client [%s] at 0x%02x\n",
  342. client->name, client->addr);
  343. }
  344. }
  345. if (client->flags & I2C_CLIENT_ALLOW_USE)
  346. client->usage_count = 0;
  347. client->dev.parent = &client->adapter->dev;
  348. client->dev.driver = &client->driver->driver;
  349. client->dev.bus = &i2c_bus_type;
  350. client->dev.release = &i2c_client_release;
  351. snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
  352. "%d-%04x", i2c_adapter_id(adapter), client->addr);
  353. dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
  354. client->name, client->dev.bus_id);
  355. device_register(&client->dev);
  356. device_create_file(&client->dev, &dev_attr_client_name);
  357. return 0;
  358. }
  359. int i2c_detach_client(struct i2c_client *client)
  360. {
  361. struct i2c_adapter *adapter = client->adapter;
  362. int res = 0;
  363. if ((client->flags & I2C_CLIENT_ALLOW_USE)
  364. && (client->usage_count > 0)) {
  365. dev_warn(&client->dev, "Client [%s] still busy, "
  366. "can't detach\n", client->name);
  367. return -EBUSY;
  368. }
  369. if (adapter->client_unregister) {
  370. res = adapter->client_unregister(client);
  371. if (res) {
  372. dev_err(&client->dev,
  373. "client_unregister [%s] failed, "
  374. "client not detached\n", client->name);
  375. goto out;
  376. }
  377. }
  378. down(&adapter->clist_lock);
  379. list_del(&client->list);
  380. init_completion(&client->released);
  381. device_remove_file(&client->dev, &dev_attr_client_name);
  382. device_unregister(&client->dev);
  383. up(&adapter->clist_lock);
  384. wait_for_completion(&client->released);
  385. out:
  386. return res;
  387. }
  388. static int i2c_inc_use_client(struct i2c_client *client)
  389. {
  390. if (!try_module_get(client->driver->owner))
  391. return -ENODEV;
  392. if (!try_module_get(client->adapter->owner)) {
  393. module_put(client->driver->owner);
  394. return -ENODEV;
  395. }
  396. return 0;
  397. }
  398. static void i2c_dec_use_client(struct i2c_client *client)
  399. {
  400. module_put(client->driver->owner);
  401. module_put(client->adapter->owner);
  402. }
  403. int i2c_use_client(struct i2c_client *client)
  404. {
  405. int ret;
  406. ret = i2c_inc_use_client(client);
  407. if (ret)
  408. return ret;
  409. if (client->flags & I2C_CLIENT_ALLOW_USE) {
  410. if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
  411. client->usage_count++;
  412. else if (client->usage_count > 0)
  413. goto busy;
  414. else
  415. client->usage_count++;
  416. }
  417. return 0;
  418. busy:
  419. i2c_dec_use_client(client);
  420. return -EBUSY;
  421. }
  422. int i2c_release_client(struct i2c_client *client)
  423. {
  424. if(client->flags & I2C_CLIENT_ALLOW_USE) {
  425. if(client->usage_count>0)
  426. client->usage_count--;
  427. else {
  428. pr_debug("i2c-core: %s used one too many times\n",
  429. __FUNCTION__);
  430. return -EPERM;
  431. }
  432. }
  433. i2c_dec_use_client(client);
  434. return 0;
  435. }
  436. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  437. {
  438. struct list_head *item;
  439. struct i2c_client *client;
  440. down(&adap->clist_lock);
  441. list_for_each(item,&adap->clients) {
  442. client = list_entry(item, struct i2c_client, list);
  443. if (!try_module_get(client->driver->owner))
  444. continue;
  445. if (NULL != client->driver->command) {
  446. up(&adap->clist_lock);
  447. client->driver->command(client,cmd,arg);
  448. down(&adap->clist_lock);
  449. }
  450. module_put(client->driver->owner);
  451. }
  452. up(&adap->clist_lock);
  453. }
  454. static int __init i2c_init(void)
  455. {
  456. int retval;
  457. retval = bus_register(&i2c_bus_type);
  458. if (retval)
  459. return retval;
  460. retval = driver_register(&i2c_adapter_driver);
  461. if (retval)
  462. return retval;
  463. return class_register(&i2c_adapter_class);
  464. }
  465. static void __exit i2c_exit(void)
  466. {
  467. class_unregister(&i2c_adapter_class);
  468. driver_unregister(&i2c_adapter_driver);
  469. bus_unregister(&i2c_bus_type);
  470. }
  471. subsys_initcall(i2c_init);
  472. module_exit(i2c_exit);
  473. /* ----------------------------------------------------
  474. * the functional interface to the i2c busses.
  475. * ----------------------------------------------------
  476. */
  477. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
  478. {
  479. int ret;
  480. if (adap->algo->master_xfer) {
  481. #ifdef DEBUG
  482. for (ret = 0; ret < num; ret++) {
  483. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  484. "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
  485. 'R' : 'W', msgs[ret].addr, msgs[ret].len);
  486. }
  487. #endif
  488. down(&adap->bus_lock);
  489. ret = adap->algo->master_xfer(adap,msgs,num);
  490. up(&adap->bus_lock);
  491. return ret;
  492. } else {
  493. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  494. return -ENOSYS;
  495. }
  496. }
  497. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  498. {
  499. int ret;
  500. struct i2c_adapter *adap=client->adapter;
  501. struct i2c_msg msg;
  502. msg.addr = client->addr;
  503. msg.flags = client->flags & I2C_M_TEN;
  504. msg.len = count;
  505. msg.buf = (char *)buf;
  506. ret = i2c_transfer(adap, &msg, 1);
  507. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  508. transmitted, else error code. */
  509. return (ret == 1) ? count : ret;
  510. }
  511. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  512. {
  513. struct i2c_adapter *adap=client->adapter;
  514. struct i2c_msg msg;
  515. int ret;
  516. msg.addr = client->addr;
  517. msg.flags = client->flags & I2C_M_TEN;
  518. msg.flags |= I2C_M_RD;
  519. msg.len = count;
  520. msg.buf = buf;
  521. ret = i2c_transfer(adap, &msg, 1);
  522. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  523. transmitted, else error code. */
  524. return (ret == 1) ? count : ret;
  525. }
  526. int i2c_control(struct i2c_client *client,
  527. unsigned int cmd, unsigned long arg)
  528. {
  529. int ret = 0;
  530. struct i2c_adapter *adap = client->adapter;
  531. dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
  532. switch (cmd) {
  533. case I2C_RETRIES:
  534. adap->retries = arg;
  535. break;
  536. case I2C_TIMEOUT:
  537. adap->timeout = arg;
  538. break;
  539. default:
  540. if (adap->algo->algo_control!=NULL)
  541. ret = adap->algo->algo_control(adap,cmd,arg);
  542. }
  543. return ret;
  544. }
  545. /* ----------------------------------------------------
  546. * the i2c address scanning function
  547. * Will not work for 10-bit addresses!
  548. * ----------------------------------------------------
  549. */
  550. static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
  551. int (*found_proc) (struct i2c_adapter *, int, int))
  552. {
  553. int err;
  554. /* Make sure the address is valid */
  555. if (addr < 0x03 || addr > 0x77) {
  556. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  557. addr);
  558. return -EINVAL;
  559. }
  560. /* Skip if already in use */
  561. if (i2c_check_addr(adapter, addr))
  562. return 0;
  563. /* Make sure there is something at this address, unless forced */
  564. if (kind < 0
  565. && i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
  566. return 0;
  567. /* Finally call the custom detection function */
  568. err = found_proc(adapter, addr, kind);
  569. /* -ENODEV can be returned if there is a chip at the given address
  570. but it isn't supported by this chip driver. We catch it here as
  571. this isn't an error. */
  572. return (err == -ENODEV) ? 0 : err;
  573. }
  574. int i2c_probe(struct i2c_adapter *adapter,
  575. struct i2c_client_address_data *address_data,
  576. int (*found_proc) (struct i2c_adapter *, int, int))
  577. {
  578. int i, err;
  579. int adap_id = i2c_adapter_id(adapter);
  580. /* Forget it if we can't probe using SMBUS_QUICK */
  581. if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
  582. return -1;
  583. /* Force entries are done first, and are not affected by ignore
  584. entries */
  585. if (address_data->forces) {
  586. unsigned short **forces = address_data->forces;
  587. int kind;
  588. for (kind = 0; forces[kind]; kind++) {
  589. for (i = 0; forces[kind][i] != I2C_CLIENT_END;
  590. i += 2) {
  591. if (forces[kind][i] == adap_id
  592. || forces[kind][i] == ANY_I2C_BUS) {
  593. dev_dbg(&adapter->dev, "found force "
  594. "parameter for adapter %d, "
  595. "addr 0x%02x, kind %d\n",
  596. adap_id, forces[kind][i + 1],
  597. kind);
  598. err = i2c_probe_address(adapter,
  599. forces[kind][i + 1],
  600. kind, found_proc);
  601. if (err)
  602. return err;
  603. }
  604. }
  605. }
  606. }
  607. /* Probe entries are done second, and are not affected by ignore
  608. entries either */
  609. for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
  610. if (address_data->probe[i] == adap_id
  611. || address_data->probe[i] == ANY_I2C_BUS) {
  612. dev_dbg(&adapter->dev, "found probe parameter for "
  613. "adapter %d, addr 0x%02x\n", adap_id,
  614. address_data->probe[i + 1]);
  615. err = i2c_probe_address(adapter,
  616. address_data->probe[i + 1],
  617. -1, found_proc);
  618. if (err)
  619. return err;
  620. }
  621. }
  622. /* Normal entries are done last, unless shadowed by an ignore entry */
  623. for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
  624. int j, ignore;
  625. ignore = 0;
  626. for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
  627. j += 2) {
  628. if ((address_data->ignore[j] == adap_id ||
  629. address_data->ignore[j] == ANY_I2C_BUS)
  630. && address_data->ignore[j + 1]
  631. == address_data->normal_i2c[i]) {
  632. dev_dbg(&adapter->dev, "found ignore "
  633. "parameter for adapter %d, "
  634. "addr 0x%02x\n", adap_id,
  635. address_data->ignore[j + 1]);
  636. }
  637. ignore = 1;
  638. break;
  639. }
  640. if (ignore)
  641. continue;
  642. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  643. "addr 0x%02x\n", adap_id,
  644. address_data->normal_i2c[i]);
  645. err = i2c_probe_address(adapter, address_data->normal_i2c[i],
  646. -1, found_proc);
  647. if (err)
  648. return err;
  649. }
  650. return 0;
  651. }
  652. struct i2c_adapter* i2c_get_adapter(int id)
  653. {
  654. struct i2c_adapter *adapter;
  655. down(&core_lists);
  656. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  657. if (adapter && !try_module_get(adapter->owner))
  658. adapter = NULL;
  659. up(&core_lists);
  660. return adapter;
  661. }
  662. void i2c_put_adapter(struct i2c_adapter *adap)
  663. {
  664. module_put(adap->owner);
  665. }
  666. /* The SMBus parts */
  667. #define POLY (0x1070U << 3)
  668. static u8
  669. crc8(u16 data)
  670. {
  671. int i;
  672. for(i = 0; i < 8; i++) {
  673. if (data & 0x8000)
  674. data = data ^ POLY;
  675. data = data << 1;
  676. }
  677. return (u8)(data >> 8);
  678. }
  679. /* CRC over count bytes in the first array plus the bytes in the rest
  680. array if it is non-null. rest[0] is the (length of rest) - 1
  681. and is included. */
  682. static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
  683. {
  684. int i;
  685. for(i = 0; i < count; i++)
  686. crc = crc8((crc ^ first[i]) << 8);
  687. if(rest != NULL)
  688. for(i = 0; i <= rest[0]; i++)
  689. crc = crc8((crc ^ rest[i]) << 8);
  690. return crc;
  691. }
  692. static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
  693. {
  694. return i2c_smbus_partial_pec(0, count, first, rest);
  695. }
  696. /* Returns new "size" (transaction type)
  697. Note that we convert byte to byte_data and byte_data to word_data
  698. rather than invent new xxx_PEC transactions. */
  699. static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
  700. union i2c_smbus_data *data)
  701. {
  702. u8 buf[3];
  703. buf[0] = addr << 1;
  704. buf[1] = command;
  705. switch(size) {
  706. case I2C_SMBUS_BYTE:
  707. data->byte = i2c_smbus_pec(2, buf, NULL);
  708. size = I2C_SMBUS_BYTE_DATA;
  709. break;
  710. case I2C_SMBUS_BYTE_DATA:
  711. buf[2] = data->byte;
  712. data->word = buf[2] ||
  713. (i2c_smbus_pec(3, buf, NULL) << 8);
  714. size = I2C_SMBUS_WORD_DATA;
  715. break;
  716. case I2C_SMBUS_WORD_DATA:
  717. /* unsupported */
  718. break;
  719. case I2C_SMBUS_BLOCK_DATA:
  720. data->block[data->block[0] + 1] =
  721. i2c_smbus_pec(2, buf, data->block);
  722. size = I2C_SMBUS_BLOCK_DATA_PEC;
  723. break;
  724. }
  725. return size;
  726. }
  727. static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
  728. union i2c_smbus_data *data)
  729. {
  730. u8 buf[3], rpec, cpec;
  731. buf[1] = command;
  732. switch(size) {
  733. case I2C_SMBUS_BYTE_DATA:
  734. buf[0] = (addr << 1) | 1;
  735. cpec = i2c_smbus_pec(2, buf, NULL);
  736. rpec = data->byte;
  737. break;
  738. case I2C_SMBUS_WORD_DATA:
  739. buf[0] = (addr << 1) | 1;
  740. buf[2] = data->word & 0xff;
  741. cpec = i2c_smbus_pec(3, buf, NULL);
  742. rpec = data->word >> 8;
  743. break;
  744. case I2C_SMBUS_WORD_DATA_PEC:
  745. /* unsupported */
  746. cpec = rpec = 0;
  747. break;
  748. case I2C_SMBUS_PROC_CALL_PEC:
  749. /* unsupported */
  750. cpec = rpec = 0;
  751. break;
  752. case I2C_SMBUS_BLOCK_DATA_PEC:
  753. buf[0] = (addr << 1);
  754. buf[2] = (addr << 1) | 1;
  755. cpec = i2c_smbus_pec(3, buf, data->block);
  756. rpec = data->block[data->block[0] + 1];
  757. break;
  758. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  759. buf[0] = (addr << 1) | 1;
  760. rpec = i2c_smbus_partial_pec(partial, 1,
  761. buf, data->block);
  762. cpec = data->block[data->block[0] + 1];
  763. break;
  764. default:
  765. cpec = rpec = 0;
  766. break;
  767. }
  768. if (rpec != cpec) {
  769. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  770. rpec, cpec);
  771. return -1;
  772. }
  773. return 0;
  774. }
  775. s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
  776. {
  777. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  778. value,0,I2C_SMBUS_QUICK,NULL);
  779. }
  780. s32 i2c_smbus_read_byte(struct i2c_client *client)
  781. {
  782. union i2c_smbus_data data;
  783. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  784. I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
  785. return -1;
  786. else
  787. return 0x0FF & data.byte;
  788. }
  789. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  790. {
  791. union i2c_smbus_data data; /* only for PEC */
  792. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  793. I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
  794. }
  795. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  796. {
  797. union i2c_smbus_data data;
  798. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  799. I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
  800. return -1;
  801. else
  802. return 0x0FF & data.byte;
  803. }
  804. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  805. {
  806. union i2c_smbus_data data;
  807. data.byte = value;
  808. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  809. I2C_SMBUS_WRITE,command,
  810. I2C_SMBUS_BYTE_DATA,&data);
  811. }
  812. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  813. {
  814. union i2c_smbus_data data;
  815. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  816. I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
  817. return -1;
  818. else
  819. return 0x0FFFF & data.word;
  820. }
  821. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  822. {
  823. union i2c_smbus_data data;
  824. data.word = value;
  825. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  826. I2C_SMBUS_WRITE,command,
  827. I2C_SMBUS_WORD_DATA,&data);
  828. }
  829. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  830. u8 length, u8 *values)
  831. {
  832. union i2c_smbus_data data;
  833. int i;
  834. if (length > I2C_SMBUS_BLOCK_MAX)
  835. length = I2C_SMBUS_BLOCK_MAX;
  836. for (i = 1; i <= length; i++)
  837. data.block[i] = values[i-1];
  838. data.block[0] = length;
  839. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  840. I2C_SMBUS_WRITE,command,
  841. I2C_SMBUS_BLOCK_DATA,&data);
  842. }
  843. /* Returns the number of read bytes */
  844. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
  845. {
  846. union i2c_smbus_data data;
  847. int i;
  848. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  849. I2C_SMBUS_READ,command,
  850. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  851. return -1;
  852. else {
  853. for (i = 1; i <= data.block[0]; i++)
  854. values[i-1] = data.block[i];
  855. return data.block[0];
  856. }
  857. }
  858. /* Simulate a SMBus command using the i2c protocol
  859. No checking of parameters is done! */
  860. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  861. unsigned short flags,
  862. char read_write, u8 command, int size,
  863. union i2c_smbus_data * data)
  864. {
  865. /* So we need to generate a series of msgs. In the case of writing, we
  866. need to use only one message; when reading, we need two. We initialize
  867. most things with sane defaults, to keep the code below somewhat
  868. simpler. */
  869. unsigned char msgbuf0[34];
  870. unsigned char msgbuf1[34];
  871. int num = read_write == I2C_SMBUS_READ?2:1;
  872. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  873. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  874. };
  875. int i;
  876. msgbuf0[0] = command;
  877. switch(size) {
  878. case I2C_SMBUS_QUICK:
  879. msg[0].len = 0;
  880. /* Special case: The read/write field is used as data */
  881. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  882. num = 1;
  883. break;
  884. case I2C_SMBUS_BYTE:
  885. if (read_write == I2C_SMBUS_READ) {
  886. /* Special case: only a read! */
  887. msg[0].flags = I2C_M_RD | flags;
  888. num = 1;
  889. }
  890. break;
  891. case I2C_SMBUS_BYTE_DATA:
  892. if (read_write == I2C_SMBUS_READ)
  893. msg[1].len = 1;
  894. else {
  895. msg[0].len = 2;
  896. msgbuf0[1] = data->byte;
  897. }
  898. break;
  899. case I2C_SMBUS_WORD_DATA:
  900. if (read_write == I2C_SMBUS_READ)
  901. msg[1].len = 2;
  902. else {
  903. msg[0].len=3;
  904. msgbuf0[1] = data->word & 0xff;
  905. msgbuf0[2] = (data->word >> 8) & 0xff;
  906. }
  907. break;
  908. case I2C_SMBUS_PROC_CALL:
  909. num = 2; /* Special case */
  910. read_write = I2C_SMBUS_READ;
  911. msg[0].len = 3;
  912. msg[1].len = 2;
  913. msgbuf0[1] = data->word & 0xff;
  914. msgbuf0[2] = (data->word >> 8) & 0xff;
  915. break;
  916. case I2C_SMBUS_BLOCK_DATA:
  917. case I2C_SMBUS_BLOCK_DATA_PEC:
  918. if (read_write == I2C_SMBUS_READ) {
  919. dev_err(&adapter->dev, "Block read not supported "
  920. "under I2C emulation!\n");
  921. return -1;
  922. } else {
  923. msg[0].len = data->block[0] + 2;
  924. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  925. dev_err(&adapter->dev, "smbus_access called with "
  926. "invalid block write size (%d)\n",
  927. data->block[0]);
  928. return -1;
  929. }
  930. if(size == I2C_SMBUS_BLOCK_DATA_PEC)
  931. (msg[0].len)++;
  932. for (i = 1; i <= msg[0].len; i++)
  933. msgbuf0[i] = data->block[i-1];
  934. }
  935. break;
  936. case I2C_SMBUS_BLOCK_PROC_CALL:
  937. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  938. dev_dbg(&adapter->dev, "Block process call not supported "
  939. "under I2C emulation!\n");
  940. return -1;
  941. case I2C_SMBUS_I2C_BLOCK_DATA:
  942. if (read_write == I2C_SMBUS_READ) {
  943. msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
  944. } else {
  945. msg[0].len = data->block[0] + 1;
  946. if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
  947. dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
  948. "invalid block write size (%d)\n",
  949. data->block[0]);
  950. return -1;
  951. }
  952. for (i = 1; i <= data->block[0]; i++)
  953. msgbuf0[i] = data->block[i];
  954. }
  955. break;
  956. default:
  957. dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
  958. size);
  959. return -1;
  960. }
  961. if (i2c_transfer(adapter, msg, num) < 0)
  962. return -1;
  963. if (read_write == I2C_SMBUS_READ)
  964. switch(size) {
  965. case I2C_SMBUS_BYTE:
  966. data->byte = msgbuf0[0];
  967. break;
  968. case I2C_SMBUS_BYTE_DATA:
  969. data->byte = msgbuf1[0];
  970. break;
  971. case I2C_SMBUS_WORD_DATA:
  972. case I2C_SMBUS_PROC_CALL:
  973. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  974. break;
  975. case I2C_SMBUS_I2C_BLOCK_DATA:
  976. /* fixed at 32 for now */
  977. data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
  978. for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
  979. data->block[i+1] = msgbuf1[i];
  980. break;
  981. }
  982. return 0;
  983. }
  984. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  985. char read_write, u8 command, int size,
  986. union i2c_smbus_data * data)
  987. {
  988. s32 res;
  989. int swpec = 0;
  990. u8 partial = 0;
  991. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  992. if((flags & I2C_CLIENT_PEC) &&
  993. !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
  994. swpec = 1;
  995. if(read_write == I2C_SMBUS_READ &&
  996. size == I2C_SMBUS_BLOCK_DATA)
  997. size = I2C_SMBUS_BLOCK_DATA_PEC;
  998. else if(size == I2C_SMBUS_PROC_CALL)
  999. size = I2C_SMBUS_PROC_CALL_PEC;
  1000. else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
  1001. i2c_smbus_add_pec(addr, command,
  1002. I2C_SMBUS_BLOCK_DATA, data);
  1003. partial = data->block[data->block[0] + 1];
  1004. size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
  1005. } else if(read_write == I2C_SMBUS_WRITE &&
  1006. size != I2C_SMBUS_QUICK &&
  1007. size != I2C_SMBUS_I2C_BLOCK_DATA)
  1008. size = i2c_smbus_add_pec(addr, command, size, data);
  1009. }
  1010. if (adapter->algo->smbus_xfer) {
  1011. down(&adapter->bus_lock);
  1012. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1013. command,size,data);
  1014. up(&adapter->bus_lock);
  1015. } else
  1016. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1017. command,size,data);
  1018. if(res >= 0 && swpec &&
  1019. size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
  1020. (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
  1021. size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
  1022. if(i2c_smbus_check_pec(addr, command, size, partial, data))
  1023. return -1;
  1024. }
  1025. return res;
  1026. }
  1027. /* Next four are needed by i2c-isa */
  1028. EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
  1029. EXPORT_SYMBOL_GPL(i2c_adapter_driver);
  1030. EXPORT_SYMBOL_GPL(i2c_adapter_class);
  1031. EXPORT_SYMBOL_GPL(i2c_bus_type);
  1032. EXPORT_SYMBOL(i2c_add_adapter);
  1033. EXPORT_SYMBOL(i2c_del_adapter);
  1034. EXPORT_SYMBOL(i2c_add_driver);
  1035. EXPORT_SYMBOL(i2c_del_driver);
  1036. EXPORT_SYMBOL(i2c_attach_client);
  1037. EXPORT_SYMBOL(i2c_detach_client);
  1038. EXPORT_SYMBOL(i2c_use_client);
  1039. EXPORT_SYMBOL(i2c_release_client);
  1040. EXPORT_SYMBOL(i2c_clients_command);
  1041. EXPORT_SYMBOL(i2c_check_addr);
  1042. EXPORT_SYMBOL(i2c_master_send);
  1043. EXPORT_SYMBOL(i2c_master_recv);
  1044. EXPORT_SYMBOL(i2c_control);
  1045. EXPORT_SYMBOL(i2c_transfer);
  1046. EXPORT_SYMBOL(i2c_get_adapter);
  1047. EXPORT_SYMBOL(i2c_put_adapter);
  1048. EXPORT_SYMBOL(i2c_probe);
  1049. EXPORT_SYMBOL(i2c_smbus_xfer);
  1050. EXPORT_SYMBOL(i2c_smbus_write_quick);
  1051. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1052. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1053. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1054. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1055. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1056. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1057. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1058. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1059. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1060. MODULE_DESCRIPTION("I2C-Bus main module");
  1061. MODULE_LICENSE("GPL");