i2c-core.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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> and
  19. Jean Delvare <khali@linux-fr.org> */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/init.h>
  26. #include <linux/idr.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/mutex.h>
  30. #include <linux/completion.h>
  31. #include <linux/hardirq.h>
  32. #include <linux/irqflags.h>
  33. #include <linux/semaphore.h>
  34. #include <asm/uaccess.h>
  35. #include "i2c-core.h"
  36. static DEFINE_MUTEX(core_lock);
  37. static DEFINE_IDR(i2c_adapter_idr);
  38. #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
  39. /* ------------------------------------------------------------------------- */
  40. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  41. const struct i2c_client *client)
  42. {
  43. while (id->name[0]) {
  44. if (strcmp(client->name, id->name) == 0)
  45. return id;
  46. id++;
  47. }
  48. return NULL;
  49. }
  50. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  51. {
  52. struct i2c_client *client = to_i2c_client(dev);
  53. struct i2c_driver *driver = to_i2c_driver(drv);
  54. /* make legacy i2c drivers bypass driver model probing entirely;
  55. * such drivers scan each i2c adapter/bus themselves.
  56. */
  57. if (!is_newstyle_driver(driver))
  58. return 0;
  59. /* match on an id table if there is one */
  60. if (driver->id_table)
  61. return i2c_match_id(driver->id_table, client) != NULL;
  62. return 0;
  63. }
  64. #ifdef CONFIG_HOTPLUG
  65. /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
  66. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  67. {
  68. struct i2c_client *client = to_i2c_client(dev);
  69. /* by definition, legacy drivers can't hotplug */
  70. if (dev->driver)
  71. return 0;
  72. if (add_uevent_var(env, "MODALIAS=%s%s",
  73. I2C_MODULE_PREFIX, client->name))
  74. return -ENOMEM;
  75. dev_dbg(dev, "uevent\n");
  76. return 0;
  77. }
  78. #else
  79. #define i2c_device_uevent NULL
  80. #endif /* CONFIG_HOTPLUG */
  81. static int i2c_device_probe(struct device *dev)
  82. {
  83. struct i2c_client *client = to_i2c_client(dev);
  84. struct i2c_driver *driver = to_i2c_driver(dev->driver);
  85. const struct i2c_device_id *id;
  86. int status;
  87. if (!driver->probe)
  88. return -ENODEV;
  89. client->driver = driver;
  90. dev_dbg(dev, "probe\n");
  91. if (driver->id_table)
  92. id = i2c_match_id(driver->id_table, client);
  93. else
  94. id = NULL;
  95. status = driver->probe(client, id);
  96. if (status)
  97. client->driver = NULL;
  98. return status;
  99. }
  100. static int i2c_device_remove(struct device *dev)
  101. {
  102. struct i2c_client *client = to_i2c_client(dev);
  103. struct i2c_driver *driver;
  104. int status;
  105. if (!dev->driver)
  106. return 0;
  107. driver = to_i2c_driver(dev->driver);
  108. if (driver->remove) {
  109. dev_dbg(dev, "remove\n");
  110. status = driver->remove(client);
  111. } else {
  112. dev->driver = NULL;
  113. status = 0;
  114. }
  115. if (status == 0)
  116. client->driver = NULL;
  117. return status;
  118. }
  119. static void i2c_device_shutdown(struct device *dev)
  120. {
  121. struct i2c_driver *driver;
  122. if (!dev->driver)
  123. return;
  124. driver = to_i2c_driver(dev->driver);
  125. if (driver->shutdown)
  126. driver->shutdown(to_i2c_client(dev));
  127. }
  128. static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
  129. {
  130. struct i2c_driver *driver;
  131. if (!dev->driver)
  132. return 0;
  133. driver = to_i2c_driver(dev->driver);
  134. if (!driver->suspend)
  135. return 0;
  136. return driver->suspend(to_i2c_client(dev), mesg);
  137. }
  138. static int i2c_device_resume(struct device * dev)
  139. {
  140. struct i2c_driver *driver;
  141. if (!dev->driver)
  142. return 0;
  143. driver = to_i2c_driver(dev->driver);
  144. if (!driver->resume)
  145. return 0;
  146. return driver->resume(to_i2c_client(dev));
  147. }
  148. static void i2c_client_release(struct device *dev)
  149. {
  150. struct i2c_client *client = to_i2c_client(dev);
  151. complete(&client->released);
  152. }
  153. static void i2c_client_dev_release(struct device *dev)
  154. {
  155. kfree(to_i2c_client(dev));
  156. }
  157. static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
  158. {
  159. struct i2c_client *client = to_i2c_client(dev);
  160. return sprintf(buf, "%s\n", client->name);
  161. }
  162. static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  163. {
  164. struct i2c_client *client = to_i2c_client(dev);
  165. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  166. }
  167. static struct device_attribute i2c_dev_attrs[] = {
  168. __ATTR(name, S_IRUGO, show_client_name, NULL),
  169. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  170. __ATTR(modalias, S_IRUGO, show_modalias, NULL),
  171. { },
  172. };
  173. static struct bus_type i2c_bus_type = {
  174. .name = "i2c",
  175. .dev_attrs = i2c_dev_attrs,
  176. .match = i2c_device_match,
  177. .uevent = i2c_device_uevent,
  178. .probe = i2c_device_probe,
  179. .remove = i2c_device_remove,
  180. .shutdown = i2c_device_shutdown,
  181. .suspend = i2c_device_suspend,
  182. .resume = i2c_device_resume,
  183. };
  184. /**
  185. * i2c_verify_client - return parameter as i2c_client, or NULL
  186. * @dev: device, probably from some driver model iterator
  187. *
  188. * When traversing the driver model tree, perhaps using driver model
  189. * iterators like @device_for_each_child(), you can't assume very much
  190. * about the nodes you find. Use this function to avoid oopses caused
  191. * by wrongly treating some non-I2C device as an i2c_client.
  192. */
  193. struct i2c_client *i2c_verify_client(struct device *dev)
  194. {
  195. return (dev->bus == &i2c_bus_type)
  196. ? to_i2c_client(dev)
  197. : NULL;
  198. }
  199. EXPORT_SYMBOL(i2c_verify_client);
  200. /**
  201. * i2c_new_device - instantiate an i2c device for use with a new style driver
  202. * @adap: the adapter managing the device
  203. * @info: describes one I2C device; bus_num is ignored
  204. * Context: can sleep
  205. *
  206. * Create a device to work with a new style i2c driver, where binding is
  207. * handled through driver model probe()/remove() methods. This call is not
  208. * appropriate for use by mainboad initialization logic, which usually runs
  209. * during an arch_initcall() long before any i2c_adapter could exist.
  210. *
  211. * This returns the new i2c client, which may be saved for later use with
  212. * i2c_unregister_device(); or NULL to indicate an error.
  213. */
  214. struct i2c_client *
  215. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  216. {
  217. struct i2c_client *client;
  218. int status;
  219. client = kzalloc(sizeof *client, GFP_KERNEL);
  220. if (!client)
  221. return NULL;
  222. client->adapter = adap;
  223. client->dev.platform_data = info->platform_data;
  224. device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
  225. client->flags = info->flags & ~I2C_CLIENT_WAKE;
  226. client->addr = info->addr;
  227. client->irq = info->irq;
  228. strlcpy(client->name, info->type, sizeof(client->name));
  229. /* a new style driver may be bound to this device when we
  230. * return from this function, or any later moment (e.g. maybe
  231. * hotplugging will load the driver module). and the device
  232. * refcount model is the standard driver model one.
  233. */
  234. status = i2c_attach_client(client);
  235. if (status < 0) {
  236. kfree(client);
  237. client = NULL;
  238. }
  239. return client;
  240. }
  241. EXPORT_SYMBOL_GPL(i2c_new_device);
  242. /**
  243. * i2c_unregister_device - reverse effect of i2c_new_device()
  244. * @client: value returned from i2c_new_device()
  245. * Context: can sleep
  246. */
  247. void i2c_unregister_device(struct i2c_client *client)
  248. {
  249. struct i2c_adapter *adapter = client->adapter;
  250. struct i2c_driver *driver = client->driver;
  251. if (driver && !is_newstyle_driver(driver)) {
  252. dev_err(&client->dev, "can't unregister devices "
  253. "with legacy drivers\n");
  254. WARN_ON(1);
  255. return;
  256. }
  257. mutex_lock(&adapter->clist_lock);
  258. list_del(&client->list);
  259. mutex_unlock(&adapter->clist_lock);
  260. device_unregister(&client->dev);
  261. }
  262. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  263. static const struct i2c_device_id dummy_id[] = {
  264. { "dummy", 0 },
  265. { },
  266. };
  267. static int dummy_probe(struct i2c_client *client,
  268. const struct i2c_device_id *id)
  269. {
  270. return 0;
  271. }
  272. static int dummy_remove(struct i2c_client *client)
  273. {
  274. return 0;
  275. }
  276. static struct i2c_driver dummy_driver = {
  277. .driver.name = "dummy",
  278. .probe = dummy_probe,
  279. .remove = dummy_remove,
  280. .id_table = dummy_id,
  281. };
  282. /**
  283. * i2c_new_dummy - return a new i2c device bound to a dummy driver
  284. * @adapter: the adapter managing the device
  285. * @address: seven bit address to be used
  286. * Context: can sleep
  287. *
  288. * This returns an I2C client bound to the "dummy" driver, intended for use
  289. * with devices that consume multiple addresses. Examples of such chips
  290. * include various EEPROMS (like 24c04 and 24c08 models).
  291. *
  292. * These dummy devices have two main uses. First, most I2C and SMBus calls
  293. * except i2c_transfer() need a client handle; the dummy will be that handle.
  294. * And second, this prevents the specified address from being bound to a
  295. * different driver.
  296. *
  297. * This returns the new i2c client, which should be saved for later use with
  298. * i2c_unregister_device(); or NULL to indicate an error.
  299. */
  300. struct i2c_client *
  301. i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
  302. {
  303. struct i2c_board_info info = {
  304. I2C_BOARD_INFO("dummy", address),
  305. };
  306. return i2c_new_device(adapter, &info);
  307. }
  308. EXPORT_SYMBOL_GPL(i2c_new_dummy);
  309. /* ------------------------------------------------------------------------- */
  310. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  311. static void i2c_adapter_dev_release(struct device *dev)
  312. {
  313. struct i2c_adapter *adap = to_i2c_adapter(dev);
  314. complete(&adap->dev_released);
  315. }
  316. static ssize_t
  317. show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
  318. {
  319. struct i2c_adapter *adap = to_i2c_adapter(dev);
  320. return sprintf(buf, "%s\n", adap->name);
  321. }
  322. static struct device_attribute i2c_adapter_attrs[] = {
  323. __ATTR(name, S_IRUGO, show_adapter_name, NULL),
  324. { },
  325. };
  326. static struct class i2c_adapter_class = {
  327. .owner = THIS_MODULE,
  328. .name = "i2c-adapter",
  329. .dev_attrs = i2c_adapter_attrs,
  330. };
  331. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  332. {
  333. struct i2c_devinfo *devinfo;
  334. mutex_lock(&__i2c_board_lock);
  335. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  336. if (devinfo->busnum == adapter->nr
  337. && !i2c_new_device(adapter,
  338. &devinfo->board_info))
  339. printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
  340. i2c_adapter_id(adapter),
  341. devinfo->board_info.addr);
  342. }
  343. mutex_unlock(&__i2c_board_lock);
  344. }
  345. static int i2c_do_add_adapter(struct device_driver *d, void *data)
  346. {
  347. struct i2c_driver *driver = to_i2c_driver(d);
  348. struct i2c_adapter *adap = data;
  349. if (driver->attach_adapter) {
  350. /* We ignore the return code; if it fails, too bad */
  351. driver->attach_adapter(adap);
  352. }
  353. return 0;
  354. }
  355. static int i2c_register_adapter(struct i2c_adapter *adap)
  356. {
  357. int res = 0, dummy;
  358. mutex_init(&adap->bus_lock);
  359. mutex_init(&adap->clist_lock);
  360. INIT_LIST_HEAD(&adap->clients);
  361. mutex_lock(&core_lock);
  362. /* Add the adapter to the driver core.
  363. * If the parent pointer is not set up,
  364. * we add this adapter to the host bus.
  365. */
  366. if (adap->dev.parent == NULL) {
  367. adap->dev.parent = &platform_bus;
  368. pr_debug("I2C adapter driver [%s] forgot to specify "
  369. "physical device\n", adap->name);
  370. }
  371. sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
  372. adap->dev.release = &i2c_adapter_dev_release;
  373. adap->dev.class = &i2c_adapter_class;
  374. res = device_register(&adap->dev);
  375. if (res)
  376. goto out_list;
  377. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  378. /* create pre-declared device nodes for new-style drivers */
  379. if (adap->nr < __i2c_first_dynamic_bus_num)
  380. i2c_scan_static_board_info(adap);
  381. /* let legacy drivers scan this bus for matching devices */
  382. dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  383. i2c_do_add_adapter);
  384. out_unlock:
  385. mutex_unlock(&core_lock);
  386. return res;
  387. out_list:
  388. idr_remove(&i2c_adapter_idr, adap->nr);
  389. goto out_unlock;
  390. }
  391. /**
  392. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  393. * @adapter: the adapter to add
  394. * Context: can sleep
  395. *
  396. * This routine is used to declare an I2C adapter when its bus number
  397. * doesn't matter. Examples: for I2C adapters dynamically added by
  398. * USB links or PCI plugin cards.
  399. *
  400. * When this returns zero, a new bus number was allocated and stored
  401. * in adap->nr, and the specified adapter became available for clients.
  402. * Otherwise, a negative errno value is returned.
  403. */
  404. int i2c_add_adapter(struct i2c_adapter *adapter)
  405. {
  406. int id, res = 0;
  407. retry:
  408. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  409. return -ENOMEM;
  410. mutex_lock(&core_lock);
  411. /* "above" here means "above or equal to", sigh */
  412. res = idr_get_new_above(&i2c_adapter_idr, adapter,
  413. __i2c_first_dynamic_bus_num, &id);
  414. mutex_unlock(&core_lock);
  415. if (res < 0) {
  416. if (res == -EAGAIN)
  417. goto retry;
  418. return res;
  419. }
  420. adapter->nr = id;
  421. return i2c_register_adapter(adapter);
  422. }
  423. EXPORT_SYMBOL(i2c_add_adapter);
  424. /**
  425. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  426. * @adap: the adapter to register (with adap->nr initialized)
  427. * Context: can sleep
  428. *
  429. * This routine is used to declare an I2C adapter when its bus number
  430. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  431. * or otherwise built in to the system's mainboard, and where i2c_board_info
  432. * is used to properly configure I2C devices.
  433. *
  434. * If no devices have pre-been declared for this bus, then be sure to
  435. * register the adapter before any dynamically allocated ones. Otherwise
  436. * the required bus ID may not be available.
  437. *
  438. * When this returns zero, the specified adapter became available for
  439. * clients using the bus number provided in adap->nr. Also, the table
  440. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  441. * and the appropriate driver model device nodes are created. Otherwise, a
  442. * negative errno value is returned.
  443. */
  444. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  445. {
  446. int id;
  447. int status;
  448. if (adap->nr & ~MAX_ID_MASK)
  449. return -EINVAL;
  450. retry:
  451. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  452. return -ENOMEM;
  453. mutex_lock(&core_lock);
  454. /* "above" here means "above or equal to", sigh;
  455. * we need the "equal to" result to force the result
  456. */
  457. status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
  458. if (status == 0 && id != adap->nr) {
  459. status = -EBUSY;
  460. idr_remove(&i2c_adapter_idr, id);
  461. }
  462. mutex_unlock(&core_lock);
  463. if (status == -EAGAIN)
  464. goto retry;
  465. if (status == 0)
  466. status = i2c_register_adapter(adap);
  467. return status;
  468. }
  469. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  470. static int i2c_do_del_adapter(struct device_driver *d, void *data)
  471. {
  472. struct i2c_driver *driver = to_i2c_driver(d);
  473. struct i2c_adapter *adapter = data;
  474. int res;
  475. if (!driver->detach_adapter)
  476. return 0;
  477. res = driver->detach_adapter(adapter);
  478. if (res)
  479. dev_err(&adapter->dev, "detach_adapter failed (%d) "
  480. "for driver [%s]\n", res, driver->driver.name);
  481. return res;
  482. }
  483. /**
  484. * i2c_del_adapter - unregister I2C adapter
  485. * @adap: the adapter being unregistered
  486. * Context: can sleep
  487. *
  488. * This unregisters an I2C adapter which was previously registered
  489. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  490. */
  491. int i2c_del_adapter(struct i2c_adapter *adap)
  492. {
  493. struct list_head *item, *_n;
  494. struct i2c_client *client;
  495. int res = 0;
  496. mutex_lock(&core_lock);
  497. /* First make sure that this adapter was ever added */
  498. if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
  499. pr_debug("i2c-core: attempting to delete unregistered "
  500. "adapter [%s]\n", adap->name);
  501. res = -EINVAL;
  502. goto out_unlock;
  503. }
  504. /* Tell drivers about this removal */
  505. res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  506. i2c_do_del_adapter);
  507. if (res)
  508. goto out_unlock;
  509. /* detach any active clients. This must be done first, because
  510. * it can fail; in which case we give up. */
  511. list_for_each_safe(item, _n, &adap->clients) {
  512. struct i2c_driver *driver;
  513. client = list_entry(item, struct i2c_client, list);
  514. driver = client->driver;
  515. /* new style, follow standard driver model */
  516. if (!driver || is_newstyle_driver(driver)) {
  517. i2c_unregister_device(client);
  518. continue;
  519. }
  520. /* legacy drivers create and remove clients themselves */
  521. if ((res = driver->detach_client(client))) {
  522. dev_err(&adap->dev, "detach_client failed for client "
  523. "[%s] at address 0x%02x\n", client->name,
  524. client->addr);
  525. goto out_unlock;
  526. }
  527. }
  528. /* clean up the sysfs representation */
  529. init_completion(&adap->dev_released);
  530. device_unregister(&adap->dev);
  531. /* wait for sysfs to drop all references */
  532. wait_for_completion(&adap->dev_released);
  533. /* free bus id */
  534. idr_remove(&i2c_adapter_idr, adap->nr);
  535. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  536. out_unlock:
  537. mutex_unlock(&core_lock);
  538. return res;
  539. }
  540. EXPORT_SYMBOL(i2c_del_adapter);
  541. /* ------------------------------------------------------------------------- */
  542. /*
  543. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  544. * i2c slave chips, on a bus instance associated with some i2c_adapter. There
  545. * are two models for binding the driver to its device: "new style" drivers
  546. * follow the standard Linux driver model and just respond to probe() calls
  547. * issued if the driver core sees they match(); "legacy" drivers create device
  548. * nodes themselves.
  549. */
  550. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  551. {
  552. int res;
  553. /* new style driver methods can't mix with legacy ones */
  554. if (is_newstyle_driver(driver)) {
  555. if (driver->attach_adapter || driver->detach_adapter
  556. || driver->detach_client) {
  557. printk(KERN_WARNING
  558. "i2c-core: driver [%s] is confused\n",
  559. driver->driver.name);
  560. return -EINVAL;
  561. }
  562. }
  563. /* add the driver to the list of i2c drivers in the driver core */
  564. driver->driver.owner = owner;
  565. driver->driver.bus = &i2c_bus_type;
  566. /* for new style drivers, when registration returns the driver core
  567. * will have called probe() for all matching-but-unbound devices.
  568. */
  569. res = driver_register(&driver->driver);
  570. if (res)
  571. return res;
  572. mutex_lock(&core_lock);
  573. pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
  574. /* legacy drivers scan i2c busses directly */
  575. if (driver->attach_adapter) {
  576. struct i2c_adapter *adapter;
  577. down(&i2c_adapter_class.sem);
  578. list_for_each_entry(adapter, &i2c_adapter_class.devices,
  579. dev.node) {
  580. driver->attach_adapter(adapter);
  581. }
  582. up(&i2c_adapter_class.sem);
  583. }
  584. mutex_unlock(&core_lock);
  585. return 0;
  586. }
  587. EXPORT_SYMBOL(i2c_register_driver);
  588. /**
  589. * i2c_del_driver - unregister I2C driver
  590. * @driver: the driver being unregistered
  591. * Context: can sleep
  592. */
  593. void i2c_del_driver(struct i2c_driver *driver)
  594. {
  595. struct list_head *item2, *_n;
  596. struct i2c_client *client;
  597. struct i2c_adapter *adap;
  598. mutex_lock(&core_lock);
  599. /* new-style driver? */
  600. if (is_newstyle_driver(driver))
  601. goto unregister;
  602. /* Have a look at each adapter, if clients of this driver are still
  603. * attached. If so, detach them to be able to kill the driver
  604. * afterwards.
  605. */
  606. down(&i2c_adapter_class.sem);
  607. list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
  608. if (driver->detach_adapter) {
  609. if (driver->detach_adapter(adap)) {
  610. dev_err(&adap->dev, "detach_adapter failed "
  611. "for driver [%s]\n",
  612. driver->driver.name);
  613. }
  614. } else {
  615. list_for_each_safe(item2, _n, &adap->clients) {
  616. client = list_entry(item2, struct i2c_client, list);
  617. if (client->driver != driver)
  618. continue;
  619. dev_dbg(&adap->dev, "detaching client [%s] "
  620. "at 0x%02x\n", client->name,
  621. client->addr);
  622. if (driver->detach_client(client)) {
  623. dev_err(&adap->dev, "detach_client "
  624. "failed for client [%s] at "
  625. "0x%02x\n", client->name,
  626. client->addr);
  627. }
  628. }
  629. }
  630. }
  631. up(&i2c_adapter_class.sem);
  632. unregister:
  633. driver_unregister(&driver->driver);
  634. pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
  635. mutex_unlock(&core_lock);
  636. }
  637. EXPORT_SYMBOL(i2c_del_driver);
  638. /* ------------------------------------------------------------------------- */
  639. static int __i2c_check_addr(struct device *dev, void *addrp)
  640. {
  641. struct i2c_client *client = i2c_verify_client(dev);
  642. int addr = *(int *)addrp;
  643. if (client && client->addr == addr)
  644. return -EBUSY;
  645. return 0;
  646. }
  647. static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
  648. {
  649. return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
  650. }
  651. int i2c_attach_client(struct i2c_client *client)
  652. {
  653. struct i2c_adapter *adapter = client->adapter;
  654. int res = 0;
  655. client->dev.parent = &client->adapter->dev;
  656. client->dev.bus = &i2c_bus_type;
  657. if (client->driver)
  658. client->dev.driver = &client->driver->driver;
  659. if (client->driver && !is_newstyle_driver(client->driver)) {
  660. client->dev.release = i2c_client_release;
  661. client->dev.uevent_suppress = 1;
  662. } else
  663. client->dev.release = i2c_client_dev_release;
  664. snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
  665. "%d-%04x", i2c_adapter_id(adapter), client->addr);
  666. res = device_register(&client->dev);
  667. if (res)
  668. goto out_err;
  669. mutex_lock(&adapter->clist_lock);
  670. list_add_tail(&client->list, &adapter->clients);
  671. mutex_unlock(&adapter->clist_lock);
  672. dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
  673. client->name, client->dev.bus_id);
  674. if (adapter->client_register) {
  675. if (adapter->client_register(client)) {
  676. dev_dbg(&adapter->dev, "client_register "
  677. "failed for client [%s] at 0x%02x\n",
  678. client->name, client->addr);
  679. }
  680. }
  681. return 0;
  682. out_err:
  683. dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
  684. "(%d)\n", client->name, client->addr, res);
  685. return res;
  686. }
  687. EXPORT_SYMBOL(i2c_attach_client);
  688. int i2c_detach_client(struct i2c_client *client)
  689. {
  690. struct i2c_adapter *adapter = client->adapter;
  691. int res = 0;
  692. if (adapter->client_unregister) {
  693. res = adapter->client_unregister(client);
  694. if (res) {
  695. dev_err(&client->dev,
  696. "client_unregister [%s] failed, "
  697. "client not detached\n", client->name);
  698. goto out;
  699. }
  700. }
  701. mutex_lock(&adapter->clist_lock);
  702. list_del(&client->list);
  703. mutex_unlock(&adapter->clist_lock);
  704. init_completion(&client->released);
  705. device_unregister(&client->dev);
  706. wait_for_completion(&client->released);
  707. out:
  708. return res;
  709. }
  710. EXPORT_SYMBOL(i2c_detach_client);
  711. /**
  712. * i2c_use_client - increments the reference count of the i2c client structure
  713. * @client: the client being referenced
  714. *
  715. * Each live reference to a client should be refcounted. The driver model does
  716. * that automatically as part of driver binding, so that most drivers don't
  717. * need to do this explicitly: they hold a reference until they're unbound
  718. * from the device.
  719. *
  720. * A pointer to the client with the incremented reference counter is returned.
  721. */
  722. struct i2c_client *i2c_use_client(struct i2c_client *client)
  723. {
  724. get_device(&client->dev);
  725. return client;
  726. }
  727. EXPORT_SYMBOL(i2c_use_client);
  728. /**
  729. * i2c_release_client - release a use of the i2c client structure
  730. * @client: the client being no longer referenced
  731. *
  732. * Must be called when a user of a client is finished with it.
  733. */
  734. void i2c_release_client(struct i2c_client *client)
  735. {
  736. put_device(&client->dev);
  737. }
  738. EXPORT_SYMBOL(i2c_release_client);
  739. struct i2c_cmd_arg {
  740. unsigned cmd;
  741. void *arg;
  742. };
  743. static int i2c_cmd(struct device *dev, void *_arg)
  744. {
  745. struct i2c_client *client = i2c_verify_client(dev);
  746. struct i2c_cmd_arg *arg = _arg;
  747. if (client && client->driver && client->driver->command)
  748. client->driver->command(client, arg->cmd, arg->arg);
  749. return 0;
  750. }
  751. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  752. {
  753. struct i2c_cmd_arg cmd_arg;
  754. cmd_arg.cmd = cmd;
  755. cmd_arg.arg = arg;
  756. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  757. }
  758. EXPORT_SYMBOL(i2c_clients_command);
  759. static int __init i2c_init(void)
  760. {
  761. int retval;
  762. retval = bus_register(&i2c_bus_type);
  763. if (retval)
  764. return retval;
  765. retval = class_register(&i2c_adapter_class);
  766. if (retval)
  767. goto bus_err;
  768. retval = i2c_add_driver(&dummy_driver);
  769. if (retval)
  770. goto class_err;
  771. return 0;
  772. class_err:
  773. class_unregister(&i2c_adapter_class);
  774. bus_err:
  775. bus_unregister(&i2c_bus_type);
  776. return retval;
  777. }
  778. static void __exit i2c_exit(void)
  779. {
  780. i2c_del_driver(&dummy_driver);
  781. class_unregister(&i2c_adapter_class);
  782. bus_unregister(&i2c_bus_type);
  783. }
  784. subsys_initcall(i2c_init);
  785. module_exit(i2c_exit);
  786. /* ----------------------------------------------------
  787. * the functional interface to the i2c busses.
  788. * ----------------------------------------------------
  789. */
  790. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
  791. {
  792. int ret;
  793. if (adap->algo->master_xfer) {
  794. #ifdef DEBUG
  795. for (ret = 0; ret < num; ret++) {
  796. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  797. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  798. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  799. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  800. }
  801. #endif
  802. if (in_atomic() || irqs_disabled()) {
  803. ret = mutex_trylock(&adap->bus_lock);
  804. if (!ret)
  805. /* I2C activity is ongoing. */
  806. return -EAGAIN;
  807. } else {
  808. mutex_lock_nested(&adap->bus_lock, adap->level);
  809. }
  810. ret = adap->algo->master_xfer(adap,msgs,num);
  811. mutex_unlock(&adap->bus_lock);
  812. return ret;
  813. } else {
  814. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  815. return -ENOSYS;
  816. }
  817. }
  818. EXPORT_SYMBOL(i2c_transfer);
  819. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  820. {
  821. int ret;
  822. struct i2c_adapter *adap=client->adapter;
  823. struct i2c_msg msg;
  824. msg.addr = client->addr;
  825. msg.flags = client->flags & I2C_M_TEN;
  826. msg.len = count;
  827. msg.buf = (char *)buf;
  828. ret = i2c_transfer(adap, &msg, 1);
  829. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  830. transmitted, else error code. */
  831. return (ret == 1) ? count : ret;
  832. }
  833. EXPORT_SYMBOL(i2c_master_send);
  834. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  835. {
  836. struct i2c_adapter *adap=client->adapter;
  837. struct i2c_msg msg;
  838. int ret;
  839. msg.addr = client->addr;
  840. msg.flags = client->flags & I2C_M_TEN;
  841. msg.flags |= I2C_M_RD;
  842. msg.len = count;
  843. msg.buf = buf;
  844. ret = i2c_transfer(adap, &msg, 1);
  845. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  846. transmitted, else error code. */
  847. return (ret == 1) ? count : ret;
  848. }
  849. EXPORT_SYMBOL(i2c_master_recv);
  850. /* ----------------------------------------------------
  851. * the i2c address scanning function
  852. * Will not work for 10-bit addresses!
  853. * ----------------------------------------------------
  854. */
  855. static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
  856. int (*found_proc) (struct i2c_adapter *, int, int))
  857. {
  858. int err;
  859. /* Make sure the address is valid */
  860. if (addr < 0x03 || addr > 0x77) {
  861. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  862. addr);
  863. return -EINVAL;
  864. }
  865. /* Skip if already in use */
  866. if (i2c_check_addr(adapter, addr))
  867. return 0;
  868. /* Make sure there is something at this address, unless forced */
  869. if (kind < 0) {
  870. if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  871. I2C_SMBUS_QUICK, NULL) < 0)
  872. return 0;
  873. /* prevent 24RF08 corruption */
  874. if ((addr & ~0x0f) == 0x50)
  875. i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  876. I2C_SMBUS_QUICK, NULL);
  877. }
  878. /* Finally call the custom detection function */
  879. err = found_proc(adapter, addr, kind);
  880. /* -ENODEV can be returned if there is a chip at the given address
  881. but it isn't supported by this chip driver. We catch it here as
  882. this isn't an error. */
  883. if (err == -ENODEV)
  884. err = 0;
  885. if (err)
  886. dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
  887. addr, err);
  888. return err;
  889. }
  890. int i2c_probe(struct i2c_adapter *adapter,
  891. const struct i2c_client_address_data *address_data,
  892. int (*found_proc) (struct i2c_adapter *, int, int))
  893. {
  894. int i, err;
  895. int adap_id = i2c_adapter_id(adapter);
  896. /* Force entries are done first, and are not affected by ignore
  897. entries */
  898. if (address_data->forces) {
  899. const unsigned short * const *forces = address_data->forces;
  900. int kind;
  901. for (kind = 0; forces[kind]; kind++) {
  902. for (i = 0; forces[kind][i] != I2C_CLIENT_END;
  903. i += 2) {
  904. if (forces[kind][i] == adap_id
  905. || forces[kind][i] == ANY_I2C_BUS) {
  906. dev_dbg(&adapter->dev, "found force "
  907. "parameter for adapter %d, "
  908. "addr 0x%02x, kind %d\n",
  909. adap_id, forces[kind][i + 1],
  910. kind);
  911. err = i2c_probe_address(adapter,
  912. forces[kind][i + 1],
  913. kind, found_proc);
  914. if (err)
  915. return err;
  916. }
  917. }
  918. }
  919. }
  920. /* Stop here if we can't use SMBUS_QUICK */
  921. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
  922. if (address_data->probe[0] == I2C_CLIENT_END
  923. && address_data->normal_i2c[0] == I2C_CLIENT_END)
  924. return 0;
  925. dev_warn(&adapter->dev, "SMBus Quick command not supported, "
  926. "can't probe for chips\n");
  927. return -1;
  928. }
  929. /* Probe entries are done second, and are not affected by ignore
  930. entries either */
  931. for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
  932. if (address_data->probe[i] == adap_id
  933. || address_data->probe[i] == ANY_I2C_BUS) {
  934. dev_dbg(&adapter->dev, "found probe parameter for "
  935. "adapter %d, addr 0x%02x\n", adap_id,
  936. address_data->probe[i + 1]);
  937. err = i2c_probe_address(adapter,
  938. address_data->probe[i + 1],
  939. -1, found_proc);
  940. if (err)
  941. return err;
  942. }
  943. }
  944. /* Normal entries are done last, unless shadowed by an ignore entry */
  945. for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
  946. int j, ignore;
  947. ignore = 0;
  948. for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
  949. j += 2) {
  950. if ((address_data->ignore[j] == adap_id ||
  951. address_data->ignore[j] == ANY_I2C_BUS)
  952. && address_data->ignore[j + 1]
  953. == address_data->normal_i2c[i]) {
  954. dev_dbg(&adapter->dev, "found ignore "
  955. "parameter for adapter %d, "
  956. "addr 0x%02x\n", adap_id,
  957. address_data->ignore[j + 1]);
  958. ignore = 1;
  959. break;
  960. }
  961. }
  962. if (ignore)
  963. continue;
  964. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  965. "addr 0x%02x\n", adap_id,
  966. address_data->normal_i2c[i]);
  967. err = i2c_probe_address(adapter, address_data->normal_i2c[i],
  968. -1, found_proc);
  969. if (err)
  970. return err;
  971. }
  972. return 0;
  973. }
  974. EXPORT_SYMBOL(i2c_probe);
  975. struct i2c_client *
  976. i2c_new_probed_device(struct i2c_adapter *adap,
  977. struct i2c_board_info *info,
  978. unsigned short const *addr_list)
  979. {
  980. int i;
  981. /* Stop here if the bus doesn't support probing */
  982. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
  983. dev_err(&adap->dev, "Probing not supported\n");
  984. return NULL;
  985. }
  986. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  987. /* Check address validity */
  988. if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
  989. dev_warn(&adap->dev, "Invalid 7-bit address "
  990. "0x%02x\n", addr_list[i]);
  991. continue;
  992. }
  993. /* Check address availability */
  994. if (i2c_check_addr(adap, addr_list[i])) {
  995. dev_dbg(&adap->dev, "Address 0x%02x already in "
  996. "use, not probing\n", addr_list[i]);
  997. continue;
  998. }
  999. /* Test address responsiveness
  1000. The default probe method is a quick write, but it is known
  1001. to corrupt the 24RF08 EEPROMs due to a state machine bug,
  1002. and could also irreversibly write-protect some EEPROMs, so
  1003. for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
  1004. read instead. Also, some bus drivers don't implement
  1005. quick write, so we fallback to a byte read it that case
  1006. too. */
  1007. if ((addr_list[i] & ~0x07) == 0x30
  1008. || (addr_list[i] & ~0x0f) == 0x50
  1009. || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
  1010. if (i2c_smbus_xfer(adap, addr_list[i], 0,
  1011. I2C_SMBUS_READ, 0,
  1012. I2C_SMBUS_BYTE, NULL) >= 0)
  1013. break;
  1014. } else {
  1015. if (i2c_smbus_xfer(adap, addr_list[i], 0,
  1016. I2C_SMBUS_WRITE, 0,
  1017. I2C_SMBUS_QUICK, NULL) >= 0)
  1018. break;
  1019. }
  1020. }
  1021. if (addr_list[i] == I2C_CLIENT_END) {
  1022. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  1023. return NULL;
  1024. }
  1025. info->addr = addr_list[i];
  1026. return i2c_new_device(adap, info);
  1027. }
  1028. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  1029. struct i2c_adapter* i2c_get_adapter(int id)
  1030. {
  1031. struct i2c_adapter *adapter;
  1032. mutex_lock(&core_lock);
  1033. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  1034. if (adapter && !try_module_get(adapter->owner))
  1035. adapter = NULL;
  1036. mutex_unlock(&core_lock);
  1037. return adapter;
  1038. }
  1039. EXPORT_SYMBOL(i2c_get_adapter);
  1040. void i2c_put_adapter(struct i2c_adapter *adap)
  1041. {
  1042. module_put(adap->owner);
  1043. }
  1044. EXPORT_SYMBOL(i2c_put_adapter);
  1045. /* The SMBus parts */
  1046. #define POLY (0x1070U << 3)
  1047. static u8
  1048. crc8(u16 data)
  1049. {
  1050. int i;
  1051. for(i = 0; i < 8; i++) {
  1052. if (data & 0x8000)
  1053. data = data ^ POLY;
  1054. data = data << 1;
  1055. }
  1056. return (u8)(data >> 8);
  1057. }
  1058. /* Incremental CRC8 over count bytes in the array pointed to by p */
  1059. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  1060. {
  1061. int i;
  1062. for(i = 0; i < count; i++)
  1063. crc = crc8((crc ^ p[i]) << 8);
  1064. return crc;
  1065. }
  1066. /* Assume a 7-bit address, which is reasonable for SMBus */
  1067. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  1068. {
  1069. /* The address will be sent first */
  1070. u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
  1071. pec = i2c_smbus_pec(pec, &addr, 1);
  1072. /* The data buffer follows */
  1073. return i2c_smbus_pec(pec, msg->buf, msg->len);
  1074. }
  1075. /* Used for write only transactions */
  1076. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  1077. {
  1078. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  1079. msg->len++;
  1080. }
  1081. /* Return <0 on CRC error
  1082. If there was a write before this read (most cases) we need to take the
  1083. partial CRC from the write part into account.
  1084. Note that this function does modify the message (we need to decrease the
  1085. message length to hide the CRC byte from the caller). */
  1086. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  1087. {
  1088. u8 rpec = msg->buf[--msg->len];
  1089. cpec = i2c_smbus_msg_pec(cpec, msg);
  1090. if (rpec != cpec) {
  1091. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  1092. rpec, cpec);
  1093. return -1;
  1094. }
  1095. return 0;
  1096. }
  1097. s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
  1098. {
  1099. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1100. value,0,I2C_SMBUS_QUICK,NULL);
  1101. }
  1102. EXPORT_SYMBOL(i2c_smbus_write_quick);
  1103. s32 i2c_smbus_read_byte(struct i2c_client *client)
  1104. {
  1105. union i2c_smbus_data data;
  1106. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1107. I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
  1108. return -1;
  1109. else
  1110. return data.byte;
  1111. }
  1112. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1113. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  1114. {
  1115. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1116. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  1117. }
  1118. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1119. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  1120. {
  1121. union i2c_smbus_data data;
  1122. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1123. I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
  1124. return -1;
  1125. else
  1126. return data.byte;
  1127. }
  1128. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1129. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  1130. {
  1131. union i2c_smbus_data data;
  1132. data.byte = value;
  1133. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1134. I2C_SMBUS_WRITE,command,
  1135. I2C_SMBUS_BYTE_DATA,&data);
  1136. }
  1137. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1138. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  1139. {
  1140. union i2c_smbus_data data;
  1141. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1142. I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
  1143. return -1;
  1144. else
  1145. return data.word;
  1146. }
  1147. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1148. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  1149. {
  1150. union i2c_smbus_data data;
  1151. data.word = value;
  1152. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1153. I2C_SMBUS_WRITE,command,
  1154. I2C_SMBUS_WORD_DATA,&data);
  1155. }
  1156. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1157. /**
  1158. * i2c_smbus_read_block_data - SMBus block read request
  1159. * @client: Handle to slave device
  1160. * @command: Command byte issued to let the slave know what data should
  1161. * be returned
  1162. * @values: Byte array into which data will be read; big enough to hold
  1163. * the data returned by the slave. SMBus allows at most 32 bytes.
  1164. *
  1165. * Returns the number of bytes read in the slave's response, else a
  1166. * negative number to indicate some kind of error.
  1167. *
  1168. * Note that using this function requires that the client's adapter support
  1169. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  1170. * support this; its emulation through I2C messaging relies on a specific
  1171. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  1172. */
  1173. s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
  1174. u8 *values)
  1175. {
  1176. union i2c_smbus_data data;
  1177. if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1178. I2C_SMBUS_READ, command,
  1179. I2C_SMBUS_BLOCK_DATA, &data))
  1180. return -1;
  1181. memcpy(values, &data.block[1], data.block[0]);
  1182. return data.block[0];
  1183. }
  1184. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  1185. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  1186. u8 length, const u8 *values)
  1187. {
  1188. union i2c_smbus_data data;
  1189. if (length > I2C_SMBUS_BLOCK_MAX)
  1190. length = I2C_SMBUS_BLOCK_MAX;
  1191. data.block[0] = length;
  1192. memcpy(&data.block[1], values, length);
  1193. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1194. I2C_SMBUS_WRITE,command,
  1195. I2C_SMBUS_BLOCK_DATA,&data);
  1196. }
  1197. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1198. /* Returns the number of read bytes */
  1199. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
  1200. u8 length, u8 *values)
  1201. {
  1202. union i2c_smbus_data data;
  1203. if (length > I2C_SMBUS_BLOCK_MAX)
  1204. length = I2C_SMBUS_BLOCK_MAX;
  1205. data.block[0] = length;
  1206. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1207. I2C_SMBUS_READ,command,
  1208. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  1209. return -1;
  1210. memcpy(values, &data.block[1], data.block[0]);
  1211. return data.block[0];
  1212. }
  1213. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1214. s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
  1215. u8 length, const u8 *values)
  1216. {
  1217. union i2c_smbus_data data;
  1218. if (length > I2C_SMBUS_BLOCK_MAX)
  1219. length = I2C_SMBUS_BLOCK_MAX;
  1220. data.block[0] = length;
  1221. memcpy(data.block + 1, values, length);
  1222. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1223. I2C_SMBUS_WRITE, command,
  1224. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1225. }
  1226. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  1227. /* Simulate a SMBus command using the i2c protocol
  1228. No checking of parameters is done! */
  1229. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  1230. unsigned short flags,
  1231. char read_write, u8 command, int size,
  1232. union i2c_smbus_data * data)
  1233. {
  1234. /* So we need to generate a series of msgs. In the case of writing, we
  1235. need to use only one message; when reading, we need two. We initialize
  1236. most things with sane defaults, to keep the code below somewhat
  1237. simpler. */
  1238. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  1239. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  1240. int num = read_write == I2C_SMBUS_READ?2:1;
  1241. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  1242. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  1243. };
  1244. int i;
  1245. u8 partial_pec = 0;
  1246. msgbuf0[0] = command;
  1247. switch(size) {
  1248. case I2C_SMBUS_QUICK:
  1249. msg[0].len = 0;
  1250. /* Special case: The read/write field is used as data */
  1251. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  1252. num = 1;
  1253. break;
  1254. case I2C_SMBUS_BYTE:
  1255. if (read_write == I2C_SMBUS_READ) {
  1256. /* Special case: only a read! */
  1257. msg[0].flags = I2C_M_RD | flags;
  1258. num = 1;
  1259. }
  1260. break;
  1261. case I2C_SMBUS_BYTE_DATA:
  1262. if (read_write == I2C_SMBUS_READ)
  1263. msg[1].len = 1;
  1264. else {
  1265. msg[0].len = 2;
  1266. msgbuf0[1] = data->byte;
  1267. }
  1268. break;
  1269. case I2C_SMBUS_WORD_DATA:
  1270. if (read_write == I2C_SMBUS_READ)
  1271. msg[1].len = 2;
  1272. else {
  1273. msg[0].len=3;
  1274. msgbuf0[1] = data->word & 0xff;
  1275. msgbuf0[2] = data->word >> 8;
  1276. }
  1277. break;
  1278. case I2C_SMBUS_PROC_CALL:
  1279. num = 2; /* Special case */
  1280. read_write = I2C_SMBUS_READ;
  1281. msg[0].len = 3;
  1282. msg[1].len = 2;
  1283. msgbuf0[1] = data->word & 0xff;
  1284. msgbuf0[2] = data->word >> 8;
  1285. break;
  1286. case I2C_SMBUS_BLOCK_DATA:
  1287. if (read_write == I2C_SMBUS_READ) {
  1288. msg[1].flags |= I2C_M_RECV_LEN;
  1289. msg[1].len = 1; /* block length will be added by
  1290. the underlying bus driver */
  1291. } else {
  1292. msg[0].len = data->block[0] + 2;
  1293. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  1294. dev_err(&adapter->dev, "smbus_access called with "
  1295. "invalid block write size (%d)\n",
  1296. data->block[0]);
  1297. return -1;
  1298. }
  1299. for (i = 1; i < msg[0].len; i++)
  1300. msgbuf0[i] = data->block[i-1];
  1301. }
  1302. break;
  1303. case I2C_SMBUS_BLOCK_PROC_CALL:
  1304. num = 2; /* Another special case */
  1305. read_write = I2C_SMBUS_READ;
  1306. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  1307. dev_err(&adapter->dev, "%s called with invalid "
  1308. "block proc call size (%d)\n", __func__,
  1309. data->block[0]);
  1310. return -1;
  1311. }
  1312. msg[0].len = data->block[0] + 2;
  1313. for (i = 1; i < msg[0].len; i++)
  1314. msgbuf0[i] = data->block[i-1];
  1315. msg[1].flags |= I2C_M_RECV_LEN;
  1316. msg[1].len = 1; /* block length will be added by
  1317. the underlying bus driver */
  1318. break;
  1319. case I2C_SMBUS_I2C_BLOCK_DATA:
  1320. if (read_write == I2C_SMBUS_READ) {
  1321. msg[1].len = data->block[0];
  1322. } else {
  1323. msg[0].len = data->block[0] + 1;
  1324. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  1325. dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
  1326. "invalid block write size (%d)\n",
  1327. data->block[0]);
  1328. return -1;
  1329. }
  1330. for (i = 1; i <= data->block[0]; i++)
  1331. msgbuf0[i] = data->block[i];
  1332. }
  1333. break;
  1334. default:
  1335. dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
  1336. size);
  1337. return -1;
  1338. }
  1339. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  1340. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  1341. if (i) {
  1342. /* Compute PEC if first message is a write */
  1343. if (!(msg[0].flags & I2C_M_RD)) {
  1344. if (num == 1) /* Write only */
  1345. i2c_smbus_add_pec(&msg[0]);
  1346. else /* Write followed by read */
  1347. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  1348. }
  1349. /* Ask for PEC if last message is a read */
  1350. if (msg[num-1].flags & I2C_M_RD)
  1351. msg[num-1].len++;
  1352. }
  1353. if (i2c_transfer(adapter, msg, num) < 0)
  1354. return -1;
  1355. /* Check PEC if last message is a read */
  1356. if (i && (msg[num-1].flags & I2C_M_RD)) {
  1357. if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
  1358. return -1;
  1359. }
  1360. if (read_write == I2C_SMBUS_READ)
  1361. switch(size) {
  1362. case I2C_SMBUS_BYTE:
  1363. data->byte = msgbuf0[0];
  1364. break;
  1365. case I2C_SMBUS_BYTE_DATA:
  1366. data->byte = msgbuf1[0];
  1367. break;
  1368. case I2C_SMBUS_WORD_DATA:
  1369. case I2C_SMBUS_PROC_CALL:
  1370. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  1371. break;
  1372. case I2C_SMBUS_I2C_BLOCK_DATA:
  1373. for (i = 0; i < data->block[0]; i++)
  1374. data->block[i+1] = msgbuf1[i];
  1375. break;
  1376. case I2C_SMBUS_BLOCK_DATA:
  1377. case I2C_SMBUS_BLOCK_PROC_CALL:
  1378. for (i = 0; i < msgbuf1[0] + 1; i++)
  1379. data->block[i] = msgbuf1[i];
  1380. break;
  1381. }
  1382. return 0;
  1383. }
  1384. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  1385. char read_write, u8 command, int size,
  1386. union i2c_smbus_data * data)
  1387. {
  1388. s32 res;
  1389. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  1390. if (adapter->algo->smbus_xfer) {
  1391. mutex_lock(&adapter->bus_lock);
  1392. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1393. command,size,data);
  1394. mutex_unlock(&adapter->bus_lock);
  1395. } else
  1396. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1397. command,size,data);
  1398. return res;
  1399. }
  1400. EXPORT_SYMBOL(i2c_smbus_xfer);
  1401. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1402. MODULE_DESCRIPTION("I2C-Bus main module");
  1403. MODULE_LICENSE("GPL");