i2c-core.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  566. I2C_SMBUS_QUICK, NULL) < 0)
  567. return 0;
  568. /* prevent 24RF08 corruption */
  569. if ((addr & ~0x0f) == 0x50)
  570. i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  571. I2C_SMBUS_QUICK, NULL);
  572. }
  573. /* Finally call the custom detection function */
  574. err = found_proc(adapter, addr, kind);
  575. /* -ENODEV can be returned if there is a chip at the given address
  576. but it isn't supported by this chip driver. We catch it here as
  577. this isn't an error. */
  578. return (err == -ENODEV) ? 0 : err;
  579. }
  580. int i2c_probe(struct i2c_adapter *adapter,
  581. struct i2c_client_address_data *address_data,
  582. int (*found_proc) (struct i2c_adapter *, int, int))
  583. {
  584. int i, err;
  585. int adap_id = i2c_adapter_id(adapter);
  586. /* Forget it if we can't probe using SMBUS_QUICK */
  587. if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
  588. return -1;
  589. /* Force entries are done first, and are not affected by ignore
  590. entries */
  591. if (address_data->forces) {
  592. unsigned short **forces = address_data->forces;
  593. int kind;
  594. for (kind = 0; forces[kind]; kind++) {
  595. for (i = 0; forces[kind][i] != I2C_CLIENT_END;
  596. i += 2) {
  597. if (forces[kind][i] == adap_id
  598. || forces[kind][i] == ANY_I2C_BUS) {
  599. dev_dbg(&adapter->dev, "found force "
  600. "parameter for adapter %d, "
  601. "addr 0x%02x, kind %d\n",
  602. adap_id, forces[kind][i + 1],
  603. kind);
  604. err = i2c_probe_address(adapter,
  605. forces[kind][i + 1],
  606. kind, found_proc);
  607. if (err)
  608. return err;
  609. }
  610. }
  611. }
  612. }
  613. /* Probe entries are done second, and are not affected by ignore
  614. entries either */
  615. for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
  616. if (address_data->probe[i] == adap_id
  617. || address_data->probe[i] == ANY_I2C_BUS) {
  618. dev_dbg(&adapter->dev, "found probe parameter for "
  619. "adapter %d, addr 0x%02x\n", adap_id,
  620. address_data->probe[i + 1]);
  621. err = i2c_probe_address(adapter,
  622. address_data->probe[i + 1],
  623. -1, found_proc);
  624. if (err)
  625. return err;
  626. }
  627. }
  628. /* Normal entries are done last, unless shadowed by an ignore entry */
  629. for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
  630. int j, ignore;
  631. ignore = 0;
  632. for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
  633. j += 2) {
  634. if ((address_data->ignore[j] == adap_id ||
  635. address_data->ignore[j] == ANY_I2C_BUS)
  636. && address_data->ignore[j + 1]
  637. == address_data->normal_i2c[i]) {
  638. dev_dbg(&adapter->dev, "found ignore "
  639. "parameter for adapter %d, "
  640. "addr 0x%02x\n", adap_id,
  641. address_data->ignore[j + 1]);
  642. }
  643. ignore = 1;
  644. break;
  645. }
  646. if (ignore)
  647. continue;
  648. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  649. "addr 0x%02x\n", adap_id,
  650. address_data->normal_i2c[i]);
  651. err = i2c_probe_address(adapter, address_data->normal_i2c[i],
  652. -1, found_proc);
  653. if (err)
  654. return err;
  655. }
  656. return 0;
  657. }
  658. struct i2c_adapter* i2c_get_adapter(int id)
  659. {
  660. struct i2c_adapter *adapter;
  661. down(&core_lists);
  662. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  663. if (adapter && !try_module_get(adapter->owner))
  664. adapter = NULL;
  665. up(&core_lists);
  666. return adapter;
  667. }
  668. void i2c_put_adapter(struct i2c_adapter *adap)
  669. {
  670. module_put(adap->owner);
  671. }
  672. /* The SMBus parts */
  673. #define POLY (0x1070U << 3)
  674. static u8
  675. crc8(u16 data)
  676. {
  677. int i;
  678. for(i = 0; i < 8; i++) {
  679. if (data & 0x8000)
  680. data = data ^ POLY;
  681. data = data << 1;
  682. }
  683. return (u8)(data >> 8);
  684. }
  685. /* CRC over count bytes in the first array plus the bytes in the rest
  686. array if it is non-null. rest[0] is the (length of rest) - 1
  687. and is included. */
  688. static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
  689. {
  690. int i;
  691. for(i = 0; i < count; i++)
  692. crc = crc8((crc ^ first[i]) << 8);
  693. if(rest != NULL)
  694. for(i = 0; i <= rest[0]; i++)
  695. crc = crc8((crc ^ rest[i]) << 8);
  696. return crc;
  697. }
  698. static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
  699. {
  700. return i2c_smbus_partial_pec(0, count, first, rest);
  701. }
  702. /* Returns new "size" (transaction type)
  703. Note that we convert byte to byte_data and byte_data to word_data
  704. rather than invent new xxx_PEC transactions. */
  705. static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
  706. union i2c_smbus_data *data)
  707. {
  708. u8 buf[3];
  709. buf[0] = addr << 1;
  710. buf[1] = command;
  711. switch(size) {
  712. case I2C_SMBUS_BYTE:
  713. data->byte = i2c_smbus_pec(2, buf, NULL);
  714. size = I2C_SMBUS_BYTE_DATA;
  715. break;
  716. case I2C_SMBUS_BYTE_DATA:
  717. buf[2] = data->byte;
  718. data->word = buf[2] ||
  719. (i2c_smbus_pec(3, buf, NULL) << 8);
  720. size = I2C_SMBUS_WORD_DATA;
  721. break;
  722. case I2C_SMBUS_WORD_DATA:
  723. /* unsupported */
  724. break;
  725. case I2C_SMBUS_BLOCK_DATA:
  726. data->block[data->block[0] + 1] =
  727. i2c_smbus_pec(2, buf, data->block);
  728. size = I2C_SMBUS_BLOCK_DATA_PEC;
  729. break;
  730. }
  731. return size;
  732. }
  733. static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
  734. union i2c_smbus_data *data)
  735. {
  736. u8 buf[3], rpec, cpec;
  737. buf[1] = command;
  738. switch(size) {
  739. case I2C_SMBUS_BYTE_DATA:
  740. buf[0] = (addr << 1) | 1;
  741. cpec = i2c_smbus_pec(2, buf, NULL);
  742. rpec = data->byte;
  743. break;
  744. case I2C_SMBUS_WORD_DATA:
  745. buf[0] = (addr << 1) | 1;
  746. buf[2] = data->word & 0xff;
  747. cpec = i2c_smbus_pec(3, buf, NULL);
  748. rpec = data->word >> 8;
  749. break;
  750. case I2C_SMBUS_WORD_DATA_PEC:
  751. /* unsupported */
  752. cpec = rpec = 0;
  753. break;
  754. case I2C_SMBUS_PROC_CALL_PEC:
  755. /* unsupported */
  756. cpec = rpec = 0;
  757. break;
  758. case I2C_SMBUS_BLOCK_DATA_PEC:
  759. buf[0] = (addr << 1);
  760. buf[2] = (addr << 1) | 1;
  761. cpec = i2c_smbus_pec(3, buf, data->block);
  762. rpec = data->block[data->block[0] + 1];
  763. break;
  764. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  765. buf[0] = (addr << 1) | 1;
  766. rpec = i2c_smbus_partial_pec(partial, 1,
  767. buf, data->block);
  768. cpec = data->block[data->block[0] + 1];
  769. break;
  770. default:
  771. cpec = rpec = 0;
  772. break;
  773. }
  774. if (rpec != cpec) {
  775. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  776. rpec, cpec);
  777. return -1;
  778. }
  779. return 0;
  780. }
  781. s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
  782. {
  783. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  784. value,0,I2C_SMBUS_QUICK,NULL);
  785. }
  786. s32 i2c_smbus_read_byte(struct i2c_client *client)
  787. {
  788. union i2c_smbus_data data;
  789. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  790. I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
  791. return -1;
  792. else
  793. return 0x0FF & data.byte;
  794. }
  795. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  796. {
  797. union i2c_smbus_data data; /* only for PEC */
  798. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  799. I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
  800. }
  801. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  802. {
  803. union i2c_smbus_data data;
  804. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  805. I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
  806. return -1;
  807. else
  808. return 0x0FF & data.byte;
  809. }
  810. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  811. {
  812. union i2c_smbus_data data;
  813. data.byte = value;
  814. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  815. I2C_SMBUS_WRITE,command,
  816. I2C_SMBUS_BYTE_DATA,&data);
  817. }
  818. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  819. {
  820. union i2c_smbus_data data;
  821. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  822. I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
  823. return -1;
  824. else
  825. return 0x0FFFF & data.word;
  826. }
  827. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  828. {
  829. union i2c_smbus_data data;
  830. data.word = value;
  831. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  832. I2C_SMBUS_WRITE,command,
  833. I2C_SMBUS_WORD_DATA,&data);
  834. }
  835. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  836. u8 length, u8 *values)
  837. {
  838. union i2c_smbus_data data;
  839. int i;
  840. if (length > I2C_SMBUS_BLOCK_MAX)
  841. length = I2C_SMBUS_BLOCK_MAX;
  842. for (i = 1; i <= length; i++)
  843. data.block[i] = values[i-1];
  844. data.block[0] = length;
  845. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  846. I2C_SMBUS_WRITE,command,
  847. I2C_SMBUS_BLOCK_DATA,&data);
  848. }
  849. /* Returns the number of read bytes */
  850. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
  851. {
  852. union i2c_smbus_data data;
  853. int i;
  854. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  855. I2C_SMBUS_READ,command,
  856. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  857. return -1;
  858. else {
  859. for (i = 1; i <= data.block[0]; i++)
  860. values[i-1] = data.block[i];
  861. return data.block[0];
  862. }
  863. }
  864. /* Simulate a SMBus command using the i2c protocol
  865. No checking of parameters is done! */
  866. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  867. unsigned short flags,
  868. char read_write, u8 command, int size,
  869. union i2c_smbus_data * data)
  870. {
  871. /* So we need to generate a series of msgs. In the case of writing, we
  872. need to use only one message; when reading, we need two. We initialize
  873. most things with sane defaults, to keep the code below somewhat
  874. simpler. */
  875. unsigned char msgbuf0[34];
  876. unsigned char msgbuf1[34];
  877. int num = read_write == I2C_SMBUS_READ?2:1;
  878. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  879. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  880. };
  881. int i;
  882. msgbuf0[0] = command;
  883. switch(size) {
  884. case I2C_SMBUS_QUICK:
  885. msg[0].len = 0;
  886. /* Special case: The read/write field is used as data */
  887. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  888. num = 1;
  889. break;
  890. case I2C_SMBUS_BYTE:
  891. if (read_write == I2C_SMBUS_READ) {
  892. /* Special case: only a read! */
  893. msg[0].flags = I2C_M_RD | flags;
  894. num = 1;
  895. }
  896. break;
  897. case I2C_SMBUS_BYTE_DATA:
  898. if (read_write == I2C_SMBUS_READ)
  899. msg[1].len = 1;
  900. else {
  901. msg[0].len = 2;
  902. msgbuf0[1] = data->byte;
  903. }
  904. break;
  905. case I2C_SMBUS_WORD_DATA:
  906. if (read_write == I2C_SMBUS_READ)
  907. msg[1].len = 2;
  908. else {
  909. msg[0].len=3;
  910. msgbuf0[1] = data->word & 0xff;
  911. msgbuf0[2] = (data->word >> 8) & 0xff;
  912. }
  913. break;
  914. case I2C_SMBUS_PROC_CALL:
  915. num = 2; /* Special case */
  916. read_write = I2C_SMBUS_READ;
  917. msg[0].len = 3;
  918. msg[1].len = 2;
  919. msgbuf0[1] = data->word & 0xff;
  920. msgbuf0[2] = (data->word >> 8) & 0xff;
  921. break;
  922. case I2C_SMBUS_BLOCK_DATA:
  923. case I2C_SMBUS_BLOCK_DATA_PEC:
  924. if (read_write == I2C_SMBUS_READ) {
  925. dev_err(&adapter->dev, "Block read not supported "
  926. "under I2C emulation!\n");
  927. return -1;
  928. } else {
  929. msg[0].len = data->block[0] + 2;
  930. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  931. dev_err(&adapter->dev, "smbus_access called with "
  932. "invalid block write size (%d)\n",
  933. data->block[0]);
  934. return -1;
  935. }
  936. if(size == I2C_SMBUS_BLOCK_DATA_PEC)
  937. (msg[0].len)++;
  938. for (i = 1; i <= msg[0].len; i++)
  939. msgbuf0[i] = data->block[i-1];
  940. }
  941. break;
  942. case I2C_SMBUS_BLOCK_PROC_CALL:
  943. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  944. dev_dbg(&adapter->dev, "Block process call not supported "
  945. "under I2C emulation!\n");
  946. return -1;
  947. case I2C_SMBUS_I2C_BLOCK_DATA:
  948. if (read_write == I2C_SMBUS_READ) {
  949. msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
  950. } else {
  951. msg[0].len = data->block[0] + 1;
  952. if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
  953. dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
  954. "invalid block write size (%d)\n",
  955. data->block[0]);
  956. return -1;
  957. }
  958. for (i = 1; i <= data->block[0]; i++)
  959. msgbuf0[i] = data->block[i];
  960. }
  961. break;
  962. default:
  963. dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
  964. size);
  965. return -1;
  966. }
  967. if (i2c_transfer(adapter, msg, num) < 0)
  968. return -1;
  969. if (read_write == I2C_SMBUS_READ)
  970. switch(size) {
  971. case I2C_SMBUS_BYTE:
  972. data->byte = msgbuf0[0];
  973. break;
  974. case I2C_SMBUS_BYTE_DATA:
  975. data->byte = msgbuf1[0];
  976. break;
  977. case I2C_SMBUS_WORD_DATA:
  978. case I2C_SMBUS_PROC_CALL:
  979. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  980. break;
  981. case I2C_SMBUS_I2C_BLOCK_DATA:
  982. /* fixed at 32 for now */
  983. data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
  984. for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
  985. data->block[i+1] = msgbuf1[i];
  986. break;
  987. }
  988. return 0;
  989. }
  990. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  991. char read_write, u8 command, int size,
  992. union i2c_smbus_data * data)
  993. {
  994. s32 res;
  995. int swpec = 0;
  996. u8 partial = 0;
  997. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  998. if((flags & I2C_CLIENT_PEC) &&
  999. !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
  1000. swpec = 1;
  1001. if(read_write == I2C_SMBUS_READ &&
  1002. size == I2C_SMBUS_BLOCK_DATA)
  1003. size = I2C_SMBUS_BLOCK_DATA_PEC;
  1004. else if(size == I2C_SMBUS_PROC_CALL)
  1005. size = I2C_SMBUS_PROC_CALL_PEC;
  1006. else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
  1007. i2c_smbus_add_pec(addr, command,
  1008. I2C_SMBUS_BLOCK_DATA, data);
  1009. partial = data->block[data->block[0] + 1];
  1010. size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
  1011. } else if(read_write == I2C_SMBUS_WRITE &&
  1012. size != I2C_SMBUS_QUICK &&
  1013. size != I2C_SMBUS_I2C_BLOCK_DATA)
  1014. size = i2c_smbus_add_pec(addr, command, size, data);
  1015. }
  1016. if (adapter->algo->smbus_xfer) {
  1017. down(&adapter->bus_lock);
  1018. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1019. command,size,data);
  1020. up(&adapter->bus_lock);
  1021. } else
  1022. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1023. command,size,data);
  1024. if(res >= 0 && swpec &&
  1025. size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
  1026. (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
  1027. size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
  1028. if(i2c_smbus_check_pec(addr, command, size, partial, data))
  1029. return -1;
  1030. }
  1031. return res;
  1032. }
  1033. /* Next four are needed by i2c-isa */
  1034. EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
  1035. EXPORT_SYMBOL_GPL(i2c_adapter_driver);
  1036. EXPORT_SYMBOL_GPL(i2c_adapter_class);
  1037. EXPORT_SYMBOL_GPL(i2c_bus_type);
  1038. EXPORT_SYMBOL(i2c_add_adapter);
  1039. EXPORT_SYMBOL(i2c_del_adapter);
  1040. EXPORT_SYMBOL(i2c_add_driver);
  1041. EXPORT_SYMBOL(i2c_del_driver);
  1042. EXPORT_SYMBOL(i2c_attach_client);
  1043. EXPORT_SYMBOL(i2c_detach_client);
  1044. EXPORT_SYMBOL(i2c_use_client);
  1045. EXPORT_SYMBOL(i2c_release_client);
  1046. EXPORT_SYMBOL(i2c_clients_command);
  1047. EXPORT_SYMBOL(i2c_check_addr);
  1048. EXPORT_SYMBOL(i2c_master_send);
  1049. EXPORT_SYMBOL(i2c_master_recv);
  1050. EXPORT_SYMBOL(i2c_control);
  1051. EXPORT_SYMBOL(i2c_transfer);
  1052. EXPORT_SYMBOL(i2c_get_adapter);
  1053. EXPORT_SYMBOL(i2c_put_adapter);
  1054. EXPORT_SYMBOL(i2c_probe);
  1055. EXPORT_SYMBOL(i2c_smbus_xfer);
  1056. EXPORT_SYMBOL(i2c_smbus_write_quick);
  1057. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1058. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1059. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1060. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1061. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1062. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1063. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1064. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1065. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1066. MODULE_DESCRIPTION("I2C-Bus main module");
  1067. MODULE_LICENSE("GPL");