i2c-core.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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/mutex.h>
  28. #include <linux/of_i2c.h>
  29. #include <linux/of_device.h>
  30. #include <linux/completion.h>
  31. #include <linux/hardirq.h>
  32. #include <linux/irqflags.h>
  33. #include <linux/rwsem.h>
  34. #include <linux/pm_runtime.h>
  35. #include <asm/uaccess.h>
  36. #include "i2c-core.h"
  37. /* core_lock protects i2c_adapter_idr, and guarantees
  38. that device detection, deletion of detected devices, and attach_adapter
  39. and detach_adapter calls are serialized */
  40. static DEFINE_MUTEX(core_lock);
  41. static DEFINE_IDR(i2c_adapter_idr);
  42. static struct device_type i2c_client_type;
  43. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
  44. /* ------------------------------------------------------------------------- */
  45. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  46. const struct i2c_client *client)
  47. {
  48. while (id->name[0]) {
  49. if (strcmp(client->name, id->name) == 0)
  50. return id;
  51. id++;
  52. }
  53. return NULL;
  54. }
  55. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  56. {
  57. struct i2c_client *client = i2c_verify_client(dev);
  58. struct i2c_driver *driver;
  59. if (!client)
  60. return 0;
  61. /* Attempt an OF style match */
  62. if (of_driver_match_device(dev, drv))
  63. return 1;
  64. driver = to_i2c_driver(drv);
  65. /* match on an id table if there is one */
  66. if (driver->id_table)
  67. return i2c_match_id(driver->id_table, client) != NULL;
  68. return 0;
  69. }
  70. #ifdef CONFIG_HOTPLUG
  71. /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
  72. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  73. {
  74. struct i2c_client *client = to_i2c_client(dev);
  75. if (add_uevent_var(env, "MODALIAS=%s%s",
  76. I2C_MODULE_PREFIX, client->name))
  77. return -ENOMEM;
  78. dev_dbg(dev, "uevent\n");
  79. return 0;
  80. }
  81. #else
  82. #define i2c_device_uevent NULL
  83. #endif /* CONFIG_HOTPLUG */
  84. static int i2c_device_probe(struct device *dev)
  85. {
  86. struct i2c_client *client = i2c_verify_client(dev);
  87. struct i2c_driver *driver;
  88. int status;
  89. if (!client)
  90. return 0;
  91. driver = to_i2c_driver(dev->driver);
  92. if (!driver->probe || !driver->id_table)
  93. return -ENODEV;
  94. client->driver = driver;
  95. if (!device_can_wakeup(&client->dev))
  96. device_init_wakeup(&client->dev,
  97. client->flags & I2C_CLIENT_WAKE);
  98. dev_dbg(dev, "probe\n");
  99. status = driver->probe(client, i2c_match_id(driver->id_table, client));
  100. if (status) {
  101. client->driver = NULL;
  102. i2c_set_clientdata(client, NULL);
  103. }
  104. return status;
  105. }
  106. static int i2c_device_remove(struct device *dev)
  107. {
  108. struct i2c_client *client = i2c_verify_client(dev);
  109. struct i2c_driver *driver;
  110. int status;
  111. if (!client || !dev->driver)
  112. return 0;
  113. driver = to_i2c_driver(dev->driver);
  114. if (driver->remove) {
  115. dev_dbg(dev, "remove\n");
  116. status = driver->remove(client);
  117. } else {
  118. dev->driver = NULL;
  119. status = 0;
  120. }
  121. if (status == 0) {
  122. client->driver = NULL;
  123. i2c_set_clientdata(client, NULL);
  124. }
  125. return status;
  126. }
  127. static void i2c_device_shutdown(struct device *dev)
  128. {
  129. struct i2c_client *client = i2c_verify_client(dev);
  130. struct i2c_driver *driver;
  131. if (!client || !dev->driver)
  132. return;
  133. driver = to_i2c_driver(dev->driver);
  134. if (driver->shutdown)
  135. driver->shutdown(client);
  136. }
  137. #ifdef CONFIG_PM_SLEEP
  138. static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
  139. {
  140. struct i2c_client *client = i2c_verify_client(dev);
  141. struct i2c_driver *driver;
  142. if (!client || !dev->driver)
  143. return 0;
  144. driver = to_i2c_driver(dev->driver);
  145. if (!driver->suspend)
  146. return 0;
  147. return driver->suspend(client, mesg);
  148. }
  149. static int i2c_legacy_resume(struct device *dev)
  150. {
  151. struct i2c_client *client = i2c_verify_client(dev);
  152. struct i2c_driver *driver;
  153. if (!client || !dev->driver)
  154. return 0;
  155. driver = to_i2c_driver(dev->driver);
  156. if (!driver->resume)
  157. return 0;
  158. return driver->resume(client);
  159. }
  160. static int i2c_device_pm_suspend(struct device *dev)
  161. {
  162. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  163. if (pm_runtime_suspended(dev))
  164. return 0;
  165. if (pm)
  166. return pm->suspend ? pm->suspend(dev) : 0;
  167. return i2c_legacy_suspend(dev, PMSG_SUSPEND);
  168. }
  169. static int i2c_device_pm_resume(struct device *dev)
  170. {
  171. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  172. int ret;
  173. if (pm)
  174. ret = pm->resume ? pm->resume(dev) : 0;
  175. else
  176. ret = i2c_legacy_resume(dev);
  177. if (!ret) {
  178. pm_runtime_disable(dev);
  179. pm_runtime_set_active(dev);
  180. pm_runtime_enable(dev);
  181. }
  182. return ret;
  183. }
  184. static int i2c_device_pm_freeze(struct device *dev)
  185. {
  186. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  187. if (pm_runtime_suspended(dev))
  188. return 0;
  189. if (pm)
  190. return pm->freeze ? pm->freeze(dev) : 0;
  191. return i2c_legacy_suspend(dev, PMSG_FREEZE);
  192. }
  193. static int i2c_device_pm_thaw(struct device *dev)
  194. {
  195. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  196. if (pm_runtime_suspended(dev))
  197. return 0;
  198. if (pm)
  199. return pm->thaw ? pm->thaw(dev) : 0;
  200. return i2c_legacy_resume(dev);
  201. }
  202. static int i2c_device_pm_poweroff(struct device *dev)
  203. {
  204. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  205. if (pm_runtime_suspended(dev))
  206. return 0;
  207. if (pm)
  208. return pm->poweroff ? pm->poweroff(dev) : 0;
  209. return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
  210. }
  211. static int i2c_device_pm_restore(struct device *dev)
  212. {
  213. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  214. int ret;
  215. if (pm)
  216. ret = pm->restore ? pm->restore(dev) : 0;
  217. else
  218. ret = i2c_legacy_resume(dev);
  219. if (!ret) {
  220. pm_runtime_disable(dev);
  221. pm_runtime_set_active(dev);
  222. pm_runtime_enable(dev);
  223. }
  224. return ret;
  225. }
  226. #else /* !CONFIG_PM_SLEEP */
  227. #define i2c_device_pm_suspend NULL
  228. #define i2c_device_pm_resume NULL
  229. #define i2c_device_pm_freeze NULL
  230. #define i2c_device_pm_thaw NULL
  231. #define i2c_device_pm_poweroff NULL
  232. #define i2c_device_pm_restore NULL
  233. #endif /* !CONFIG_PM_SLEEP */
  234. static void i2c_client_dev_release(struct device *dev)
  235. {
  236. kfree(to_i2c_client(dev));
  237. }
  238. static ssize_t
  239. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  240. {
  241. return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
  242. to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
  243. }
  244. static ssize_t
  245. show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  246. {
  247. struct i2c_client *client = to_i2c_client(dev);
  248. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  249. }
  250. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  251. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  252. static struct attribute *i2c_dev_attrs[] = {
  253. &dev_attr_name.attr,
  254. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  255. &dev_attr_modalias.attr,
  256. NULL
  257. };
  258. static struct attribute_group i2c_dev_attr_group = {
  259. .attrs = i2c_dev_attrs,
  260. };
  261. static const struct attribute_group *i2c_dev_attr_groups[] = {
  262. &i2c_dev_attr_group,
  263. NULL
  264. };
  265. static const struct dev_pm_ops i2c_device_pm_ops = {
  266. .suspend = i2c_device_pm_suspend,
  267. .resume = i2c_device_pm_resume,
  268. .freeze = i2c_device_pm_freeze,
  269. .thaw = i2c_device_pm_thaw,
  270. .poweroff = i2c_device_pm_poweroff,
  271. .restore = i2c_device_pm_restore,
  272. SET_RUNTIME_PM_OPS(
  273. pm_generic_runtime_suspend,
  274. pm_generic_runtime_resume,
  275. pm_generic_runtime_idle
  276. )
  277. };
  278. struct bus_type i2c_bus_type = {
  279. .name = "i2c",
  280. .match = i2c_device_match,
  281. .probe = i2c_device_probe,
  282. .remove = i2c_device_remove,
  283. .shutdown = i2c_device_shutdown,
  284. .pm = &i2c_device_pm_ops,
  285. };
  286. EXPORT_SYMBOL_GPL(i2c_bus_type);
  287. static struct device_type i2c_client_type = {
  288. .groups = i2c_dev_attr_groups,
  289. .uevent = i2c_device_uevent,
  290. .release = i2c_client_dev_release,
  291. };
  292. /**
  293. * i2c_verify_client - return parameter as i2c_client, or NULL
  294. * @dev: device, probably from some driver model iterator
  295. *
  296. * When traversing the driver model tree, perhaps using driver model
  297. * iterators like @device_for_each_child(), you can't assume very much
  298. * about the nodes you find. Use this function to avoid oopses caused
  299. * by wrongly treating some non-I2C device as an i2c_client.
  300. */
  301. struct i2c_client *i2c_verify_client(struct device *dev)
  302. {
  303. return (dev->type == &i2c_client_type)
  304. ? to_i2c_client(dev)
  305. : NULL;
  306. }
  307. EXPORT_SYMBOL(i2c_verify_client);
  308. /* This is a permissive address validity check, I2C address map constraints
  309. * are purposedly not enforced, except for the general call address. */
  310. static int i2c_check_client_addr_validity(const struct i2c_client *client)
  311. {
  312. if (client->flags & I2C_CLIENT_TEN) {
  313. /* 10-bit address, all values are valid */
  314. if (client->addr > 0x3ff)
  315. return -EINVAL;
  316. } else {
  317. /* 7-bit address, reject the general call address */
  318. if (client->addr == 0x00 || client->addr > 0x7f)
  319. return -EINVAL;
  320. }
  321. return 0;
  322. }
  323. /* And this is a strict address validity check, used when probing. If a
  324. * device uses a reserved address, then it shouldn't be probed. 7-bit
  325. * addressing is assumed, 10-bit address devices are rare and should be
  326. * explicitly enumerated. */
  327. static int i2c_check_addr_validity(unsigned short addr)
  328. {
  329. /*
  330. * Reserved addresses per I2C specification:
  331. * 0x00 General call address / START byte
  332. * 0x01 CBUS address
  333. * 0x02 Reserved for different bus format
  334. * 0x03 Reserved for future purposes
  335. * 0x04-0x07 Hs-mode master code
  336. * 0x78-0x7b 10-bit slave addressing
  337. * 0x7c-0x7f Reserved for future purposes
  338. */
  339. if (addr < 0x08 || addr > 0x77)
  340. return -EINVAL;
  341. return 0;
  342. }
  343. static int __i2c_check_addr_busy(struct device *dev, void *addrp)
  344. {
  345. struct i2c_client *client = i2c_verify_client(dev);
  346. int addr = *(int *)addrp;
  347. if (client && client->addr == addr)
  348. return -EBUSY;
  349. return 0;
  350. }
  351. static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
  352. {
  353. return device_for_each_child(&adapter->dev, &addr,
  354. __i2c_check_addr_busy);
  355. }
  356. /**
  357. * i2c_lock_adapter - Get exclusive access to an I2C bus segment
  358. * @adapter: Target I2C bus segment
  359. */
  360. void i2c_lock_adapter(struct i2c_adapter *adapter)
  361. {
  362. rt_mutex_lock(&adapter->bus_lock);
  363. }
  364. EXPORT_SYMBOL_GPL(i2c_lock_adapter);
  365. /**
  366. * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
  367. * @adapter: Target I2C bus segment
  368. */
  369. static int i2c_trylock_adapter(struct i2c_adapter *adapter)
  370. {
  371. return rt_mutex_trylock(&adapter->bus_lock);
  372. }
  373. /**
  374. * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
  375. * @adapter: Target I2C bus segment
  376. */
  377. void i2c_unlock_adapter(struct i2c_adapter *adapter)
  378. {
  379. rt_mutex_unlock(&adapter->bus_lock);
  380. }
  381. EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
  382. /**
  383. * i2c_new_device - instantiate an i2c device
  384. * @adap: the adapter managing the device
  385. * @info: describes one I2C device; bus_num is ignored
  386. * Context: can sleep
  387. *
  388. * Create an i2c device. Binding is handled through driver model
  389. * probe()/remove() methods. A driver may be bound to this device when we
  390. * return from this function, or any later moment (e.g. maybe hotplugging will
  391. * load the driver module). This call is not appropriate for use by mainboard
  392. * initialization logic, which usually runs during an arch_initcall() long
  393. * before any i2c_adapter could exist.
  394. *
  395. * This returns the new i2c client, which may be saved for later use with
  396. * i2c_unregister_device(); or NULL to indicate an error.
  397. */
  398. struct i2c_client *
  399. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  400. {
  401. struct i2c_client *client;
  402. int status;
  403. client = kzalloc(sizeof *client, GFP_KERNEL);
  404. if (!client)
  405. return NULL;
  406. client->adapter = adap;
  407. client->dev.platform_data = info->platform_data;
  408. if (info->archdata)
  409. client->dev.archdata = *info->archdata;
  410. client->flags = info->flags;
  411. client->addr = info->addr;
  412. client->irq = info->irq;
  413. strlcpy(client->name, info->type, sizeof(client->name));
  414. /* Check for address validity */
  415. status = i2c_check_client_addr_validity(client);
  416. if (status) {
  417. dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
  418. client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
  419. goto out_err_silent;
  420. }
  421. /* Check for address business */
  422. status = i2c_check_addr_busy(adap, client->addr);
  423. if (status)
  424. goto out_err;
  425. client->dev.parent = &client->adapter->dev;
  426. client->dev.bus = &i2c_bus_type;
  427. client->dev.type = &i2c_client_type;
  428. #ifdef CONFIG_OF
  429. client->dev.of_node = info->of_node;
  430. #endif
  431. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
  432. client->addr);
  433. status = device_register(&client->dev);
  434. if (status)
  435. goto out_err;
  436. dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  437. client->name, dev_name(&client->dev));
  438. return client;
  439. out_err:
  440. dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
  441. "(%d)\n", client->name, client->addr, status);
  442. out_err_silent:
  443. kfree(client);
  444. return NULL;
  445. }
  446. EXPORT_SYMBOL_GPL(i2c_new_device);
  447. /**
  448. * i2c_unregister_device - reverse effect of i2c_new_device()
  449. * @client: value returned from i2c_new_device()
  450. * Context: can sleep
  451. */
  452. void i2c_unregister_device(struct i2c_client *client)
  453. {
  454. device_unregister(&client->dev);
  455. }
  456. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  457. static const struct i2c_device_id dummy_id[] = {
  458. { "dummy", 0 },
  459. { },
  460. };
  461. static int dummy_probe(struct i2c_client *client,
  462. const struct i2c_device_id *id)
  463. {
  464. return 0;
  465. }
  466. static int dummy_remove(struct i2c_client *client)
  467. {
  468. return 0;
  469. }
  470. static struct i2c_driver dummy_driver = {
  471. .driver.name = "dummy",
  472. .probe = dummy_probe,
  473. .remove = dummy_remove,
  474. .id_table = dummy_id,
  475. };
  476. /**
  477. * i2c_new_dummy - return a new i2c device bound to a dummy driver
  478. * @adapter: the adapter managing the device
  479. * @address: seven bit address to be used
  480. * Context: can sleep
  481. *
  482. * This returns an I2C client bound to the "dummy" driver, intended for use
  483. * with devices that consume multiple addresses. Examples of such chips
  484. * include various EEPROMS (like 24c04 and 24c08 models).
  485. *
  486. * These dummy devices have two main uses. First, most I2C and SMBus calls
  487. * except i2c_transfer() need a client handle; the dummy will be that handle.
  488. * And second, this prevents the specified address from being bound to a
  489. * different driver.
  490. *
  491. * This returns the new i2c client, which should be saved for later use with
  492. * i2c_unregister_device(); or NULL to indicate an error.
  493. */
  494. struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
  495. {
  496. struct i2c_board_info info = {
  497. I2C_BOARD_INFO("dummy", address),
  498. };
  499. return i2c_new_device(adapter, &info);
  500. }
  501. EXPORT_SYMBOL_GPL(i2c_new_dummy);
  502. /* ------------------------------------------------------------------------- */
  503. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  504. static void i2c_adapter_dev_release(struct device *dev)
  505. {
  506. struct i2c_adapter *adap = to_i2c_adapter(dev);
  507. complete(&adap->dev_released);
  508. }
  509. /*
  510. * Let users instantiate I2C devices through sysfs. This can be used when
  511. * platform initialization code doesn't contain the proper data for
  512. * whatever reason. Also useful for drivers that do device detection and
  513. * detection fails, either because the device uses an unexpected address,
  514. * or this is a compatible device with different ID register values.
  515. *
  516. * Parameter checking may look overzealous, but we really don't want
  517. * the user to provide incorrect parameters.
  518. */
  519. static ssize_t
  520. i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
  521. const char *buf, size_t count)
  522. {
  523. struct i2c_adapter *adap = to_i2c_adapter(dev);
  524. struct i2c_board_info info;
  525. struct i2c_client *client;
  526. char *blank, end;
  527. int res;
  528. dev_warn(dev, "The new_device interface is still experimental "
  529. "and may change in a near future\n");
  530. memset(&info, 0, sizeof(struct i2c_board_info));
  531. blank = strchr(buf, ' ');
  532. if (!blank) {
  533. dev_err(dev, "%s: Missing parameters\n", "new_device");
  534. return -EINVAL;
  535. }
  536. if (blank - buf > I2C_NAME_SIZE - 1) {
  537. dev_err(dev, "%s: Invalid device name\n", "new_device");
  538. return -EINVAL;
  539. }
  540. memcpy(info.type, buf, blank - buf);
  541. /* Parse remaining parameters, reject extra parameters */
  542. res = sscanf(++blank, "%hi%c", &info.addr, &end);
  543. if (res < 1) {
  544. dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
  545. return -EINVAL;
  546. }
  547. if (res > 1 && end != '\n') {
  548. dev_err(dev, "%s: Extra parameters\n", "new_device");
  549. return -EINVAL;
  550. }
  551. client = i2c_new_device(adap, &info);
  552. if (!client)
  553. return -EINVAL;
  554. /* Keep track of the added device */
  555. i2c_lock_adapter(adap);
  556. list_add_tail(&client->detected, &adap->userspace_clients);
  557. i2c_unlock_adapter(adap);
  558. dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
  559. info.type, info.addr);
  560. return count;
  561. }
  562. /*
  563. * And of course let the users delete the devices they instantiated, if
  564. * they got it wrong. This interface can only be used to delete devices
  565. * instantiated by i2c_sysfs_new_device above. This guarantees that we
  566. * don't delete devices to which some kernel code still has references.
  567. *
  568. * Parameter checking may look overzealous, but we really don't want
  569. * the user to delete the wrong device.
  570. */
  571. static ssize_t
  572. i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
  573. const char *buf, size_t count)
  574. {
  575. struct i2c_adapter *adap = to_i2c_adapter(dev);
  576. struct i2c_client *client, *next;
  577. unsigned short addr;
  578. char end;
  579. int res;
  580. /* Parse parameters, reject extra parameters */
  581. res = sscanf(buf, "%hi%c", &addr, &end);
  582. if (res < 1) {
  583. dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
  584. return -EINVAL;
  585. }
  586. if (res > 1 && end != '\n') {
  587. dev_err(dev, "%s: Extra parameters\n", "delete_device");
  588. return -EINVAL;
  589. }
  590. /* Make sure the device was added through sysfs */
  591. res = -ENOENT;
  592. i2c_lock_adapter(adap);
  593. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  594. detected) {
  595. if (client->addr == addr) {
  596. dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
  597. "delete_device", client->name, client->addr);
  598. list_del(&client->detected);
  599. i2c_unregister_device(client);
  600. res = count;
  601. break;
  602. }
  603. }
  604. i2c_unlock_adapter(adap);
  605. if (res < 0)
  606. dev_err(dev, "%s: Can't find device in list\n",
  607. "delete_device");
  608. return res;
  609. }
  610. static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
  611. static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
  612. static struct attribute *i2c_adapter_attrs[] = {
  613. &dev_attr_name.attr,
  614. &dev_attr_new_device.attr,
  615. &dev_attr_delete_device.attr,
  616. NULL
  617. };
  618. static struct attribute_group i2c_adapter_attr_group = {
  619. .attrs = i2c_adapter_attrs,
  620. };
  621. static const struct attribute_group *i2c_adapter_attr_groups[] = {
  622. &i2c_adapter_attr_group,
  623. NULL
  624. };
  625. static struct device_type i2c_adapter_type = {
  626. .groups = i2c_adapter_attr_groups,
  627. .release = i2c_adapter_dev_release,
  628. };
  629. #ifdef CONFIG_I2C_COMPAT
  630. static struct class_compat *i2c_adapter_compat_class;
  631. #endif
  632. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  633. {
  634. struct i2c_devinfo *devinfo;
  635. down_read(&__i2c_board_lock);
  636. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  637. if (devinfo->busnum == adapter->nr
  638. && !i2c_new_device(adapter,
  639. &devinfo->board_info))
  640. dev_err(&adapter->dev,
  641. "Can't create device at 0x%02x\n",
  642. devinfo->board_info.addr);
  643. }
  644. up_read(&__i2c_board_lock);
  645. }
  646. static int i2c_do_add_adapter(struct i2c_driver *driver,
  647. struct i2c_adapter *adap)
  648. {
  649. /* Detect supported devices on that bus, and instantiate them */
  650. i2c_detect(adap, driver);
  651. /* Let legacy drivers scan this bus for matching devices */
  652. if (driver->attach_adapter) {
  653. /* We ignore the return code; if it fails, too bad */
  654. driver->attach_adapter(adap);
  655. }
  656. return 0;
  657. }
  658. static int __process_new_adapter(struct device_driver *d, void *data)
  659. {
  660. return i2c_do_add_adapter(to_i2c_driver(d), data);
  661. }
  662. static int i2c_register_adapter(struct i2c_adapter *adap)
  663. {
  664. int res = 0;
  665. /* Can't register until after driver model init */
  666. if (unlikely(WARN_ON(!i2c_bus_type.p))) {
  667. res = -EAGAIN;
  668. goto out_list;
  669. }
  670. rt_mutex_init(&adap->bus_lock);
  671. INIT_LIST_HEAD(&adap->userspace_clients);
  672. /* Set default timeout to 1 second if not already set */
  673. if (adap->timeout == 0)
  674. adap->timeout = HZ;
  675. dev_set_name(&adap->dev, "i2c-%d", adap->nr);
  676. adap->dev.bus = &i2c_bus_type;
  677. adap->dev.type = &i2c_adapter_type;
  678. res = device_register(&adap->dev);
  679. if (res)
  680. goto out_list;
  681. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  682. #ifdef CONFIG_I2C_COMPAT
  683. res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
  684. adap->dev.parent);
  685. if (res)
  686. dev_warn(&adap->dev,
  687. "Failed to create compatibility class link\n");
  688. #endif
  689. /* create pre-declared device nodes */
  690. if (adap->nr < __i2c_first_dynamic_bus_num)
  691. i2c_scan_static_board_info(adap);
  692. /* Register devices from the device tree */
  693. of_i2c_register_devices(adap);
  694. /* Notify drivers */
  695. mutex_lock(&core_lock);
  696. bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
  697. mutex_unlock(&core_lock);
  698. return 0;
  699. out_list:
  700. mutex_lock(&core_lock);
  701. idr_remove(&i2c_adapter_idr, adap->nr);
  702. mutex_unlock(&core_lock);
  703. return res;
  704. }
  705. /**
  706. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  707. * @adapter: the adapter to add
  708. * Context: can sleep
  709. *
  710. * This routine is used to declare an I2C adapter when its bus number
  711. * doesn't matter. Examples: for I2C adapters dynamically added by
  712. * USB links or PCI plugin cards.
  713. *
  714. * When this returns zero, a new bus number was allocated and stored
  715. * in adap->nr, and the specified adapter became available for clients.
  716. * Otherwise, a negative errno value is returned.
  717. */
  718. int i2c_add_adapter(struct i2c_adapter *adapter)
  719. {
  720. int id, res = 0;
  721. retry:
  722. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  723. return -ENOMEM;
  724. mutex_lock(&core_lock);
  725. /* "above" here means "above or equal to", sigh */
  726. res = idr_get_new_above(&i2c_adapter_idr, adapter,
  727. __i2c_first_dynamic_bus_num, &id);
  728. mutex_unlock(&core_lock);
  729. if (res < 0) {
  730. if (res == -EAGAIN)
  731. goto retry;
  732. return res;
  733. }
  734. adapter->nr = id;
  735. return i2c_register_adapter(adapter);
  736. }
  737. EXPORT_SYMBOL(i2c_add_adapter);
  738. /**
  739. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  740. * @adap: the adapter to register (with adap->nr initialized)
  741. * Context: can sleep
  742. *
  743. * This routine is used to declare an I2C adapter when its bus number
  744. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  745. * or otherwise built in to the system's mainboard, and where i2c_board_info
  746. * is used to properly configure I2C devices.
  747. *
  748. * If no devices have pre-been declared for this bus, then be sure to
  749. * register the adapter before any dynamically allocated ones. Otherwise
  750. * the required bus ID may not be available.
  751. *
  752. * When this returns zero, the specified adapter became available for
  753. * clients using the bus number provided in adap->nr. Also, the table
  754. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  755. * and the appropriate driver model device nodes are created. Otherwise, a
  756. * negative errno value is returned.
  757. */
  758. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  759. {
  760. int id;
  761. int status;
  762. if (adap->nr & ~MAX_ID_MASK)
  763. return -EINVAL;
  764. retry:
  765. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  766. return -ENOMEM;
  767. mutex_lock(&core_lock);
  768. /* "above" here means "above or equal to", sigh;
  769. * we need the "equal to" result to force the result
  770. */
  771. status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
  772. if (status == 0 && id != adap->nr) {
  773. status = -EBUSY;
  774. idr_remove(&i2c_adapter_idr, id);
  775. }
  776. mutex_unlock(&core_lock);
  777. if (status == -EAGAIN)
  778. goto retry;
  779. if (status == 0)
  780. status = i2c_register_adapter(adap);
  781. return status;
  782. }
  783. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  784. static int i2c_do_del_adapter(struct i2c_driver *driver,
  785. struct i2c_adapter *adapter)
  786. {
  787. struct i2c_client *client, *_n;
  788. int res;
  789. /* Remove the devices we created ourselves as the result of hardware
  790. * probing (using a driver's detect method) */
  791. list_for_each_entry_safe(client, _n, &driver->clients, detected) {
  792. if (client->adapter == adapter) {
  793. dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
  794. client->name, client->addr);
  795. list_del(&client->detected);
  796. i2c_unregister_device(client);
  797. }
  798. }
  799. if (!driver->detach_adapter)
  800. return 0;
  801. res = driver->detach_adapter(adapter);
  802. if (res)
  803. dev_err(&adapter->dev, "detach_adapter failed (%d) "
  804. "for driver [%s]\n", res, driver->driver.name);
  805. return res;
  806. }
  807. static int __unregister_client(struct device *dev, void *dummy)
  808. {
  809. struct i2c_client *client = i2c_verify_client(dev);
  810. if (client)
  811. i2c_unregister_device(client);
  812. return 0;
  813. }
  814. static int __process_removed_adapter(struct device_driver *d, void *data)
  815. {
  816. return i2c_do_del_adapter(to_i2c_driver(d), data);
  817. }
  818. /**
  819. * i2c_del_adapter - unregister I2C adapter
  820. * @adap: the adapter being unregistered
  821. * Context: can sleep
  822. *
  823. * This unregisters an I2C adapter which was previously registered
  824. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  825. */
  826. int i2c_del_adapter(struct i2c_adapter *adap)
  827. {
  828. int res = 0;
  829. struct i2c_adapter *found;
  830. struct i2c_client *client, *next;
  831. /* First make sure that this adapter was ever added */
  832. mutex_lock(&core_lock);
  833. found = idr_find(&i2c_adapter_idr, adap->nr);
  834. mutex_unlock(&core_lock);
  835. if (found != adap) {
  836. pr_debug("i2c-core: attempting to delete unregistered "
  837. "adapter [%s]\n", adap->name);
  838. return -EINVAL;
  839. }
  840. /* Tell drivers about this removal */
  841. mutex_lock(&core_lock);
  842. res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  843. __process_removed_adapter);
  844. mutex_unlock(&core_lock);
  845. if (res)
  846. return res;
  847. /* Remove devices instantiated from sysfs */
  848. i2c_lock_adapter(adap);
  849. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  850. detected) {
  851. dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
  852. client->addr);
  853. list_del(&client->detected);
  854. i2c_unregister_device(client);
  855. }
  856. i2c_unlock_adapter(adap);
  857. /* Detach any active clients. This can't fail, thus we do not
  858. checking the returned value. */
  859. res = device_for_each_child(&adap->dev, NULL, __unregister_client);
  860. #ifdef CONFIG_I2C_COMPAT
  861. class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
  862. adap->dev.parent);
  863. #endif
  864. /* device name is gone after device_unregister */
  865. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  866. /* clean up the sysfs representation */
  867. init_completion(&adap->dev_released);
  868. device_unregister(&adap->dev);
  869. /* wait for sysfs to drop all references */
  870. wait_for_completion(&adap->dev_released);
  871. /* free bus id */
  872. mutex_lock(&core_lock);
  873. idr_remove(&i2c_adapter_idr, adap->nr);
  874. mutex_unlock(&core_lock);
  875. /* Clear the device structure in case this adapter is ever going to be
  876. added again */
  877. memset(&adap->dev, 0, sizeof(adap->dev));
  878. return 0;
  879. }
  880. EXPORT_SYMBOL(i2c_del_adapter);
  881. /* ------------------------------------------------------------------------- */
  882. static int __process_new_driver(struct device *dev, void *data)
  883. {
  884. if (dev->type != &i2c_adapter_type)
  885. return 0;
  886. return i2c_do_add_adapter(data, to_i2c_adapter(dev));
  887. }
  888. /*
  889. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  890. * i2c slave chips, on a bus instance associated with some i2c_adapter.
  891. */
  892. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  893. {
  894. int res;
  895. /* Can't register until after driver model init */
  896. if (unlikely(WARN_ON(!i2c_bus_type.p)))
  897. return -EAGAIN;
  898. /* add the driver to the list of i2c drivers in the driver core */
  899. driver->driver.owner = owner;
  900. driver->driver.bus = &i2c_bus_type;
  901. /* When registration returns, the driver core
  902. * will have called probe() for all matching-but-unbound devices.
  903. */
  904. res = driver_register(&driver->driver);
  905. if (res)
  906. return res;
  907. pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
  908. INIT_LIST_HEAD(&driver->clients);
  909. /* Walk the adapters that are already present */
  910. mutex_lock(&core_lock);
  911. bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
  912. mutex_unlock(&core_lock);
  913. return 0;
  914. }
  915. EXPORT_SYMBOL(i2c_register_driver);
  916. static int __process_removed_driver(struct device *dev, void *data)
  917. {
  918. if (dev->type != &i2c_adapter_type)
  919. return 0;
  920. return i2c_do_del_adapter(data, to_i2c_adapter(dev));
  921. }
  922. /**
  923. * i2c_del_driver - unregister I2C driver
  924. * @driver: the driver being unregistered
  925. * Context: can sleep
  926. */
  927. void i2c_del_driver(struct i2c_driver *driver)
  928. {
  929. mutex_lock(&core_lock);
  930. bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
  931. mutex_unlock(&core_lock);
  932. driver_unregister(&driver->driver);
  933. pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
  934. }
  935. EXPORT_SYMBOL(i2c_del_driver);
  936. /* ------------------------------------------------------------------------- */
  937. /**
  938. * i2c_use_client - increments the reference count of the i2c client structure
  939. * @client: the client being referenced
  940. *
  941. * Each live reference to a client should be refcounted. The driver model does
  942. * that automatically as part of driver binding, so that most drivers don't
  943. * need to do this explicitly: they hold a reference until they're unbound
  944. * from the device.
  945. *
  946. * A pointer to the client with the incremented reference counter is returned.
  947. */
  948. struct i2c_client *i2c_use_client(struct i2c_client *client)
  949. {
  950. if (client && get_device(&client->dev))
  951. return client;
  952. return NULL;
  953. }
  954. EXPORT_SYMBOL(i2c_use_client);
  955. /**
  956. * i2c_release_client - release a use of the i2c client structure
  957. * @client: the client being no longer referenced
  958. *
  959. * Must be called when a user of a client is finished with it.
  960. */
  961. void i2c_release_client(struct i2c_client *client)
  962. {
  963. if (client)
  964. put_device(&client->dev);
  965. }
  966. EXPORT_SYMBOL(i2c_release_client);
  967. struct i2c_cmd_arg {
  968. unsigned cmd;
  969. void *arg;
  970. };
  971. static int i2c_cmd(struct device *dev, void *_arg)
  972. {
  973. struct i2c_client *client = i2c_verify_client(dev);
  974. struct i2c_cmd_arg *arg = _arg;
  975. if (client && client->driver && client->driver->command)
  976. client->driver->command(client, arg->cmd, arg->arg);
  977. return 0;
  978. }
  979. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  980. {
  981. struct i2c_cmd_arg cmd_arg;
  982. cmd_arg.cmd = cmd;
  983. cmd_arg.arg = arg;
  984. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  985. }
  986. EXPORT_SYMBOL(i2c_clients_command);
  987. static int __init i2c_init(void)
  988. {
  989. int retval;
  990. retval = bus_register(&i2c_bus_type);
  991. if (retval)
  992. return retval;
  993. #ifdef CONFIG_I2C_COMPAT
  994. i2c_adapter_compat_class = class_compat_register("i2c-adapter");
  995. if (!i2c_adapter_compat_class) {
  996. retval = -ENOMEM;
  997. goto bus_err;
  998. }
  999. #endif
  1000. retval = i2c_add_driver(&dummy_driver);
  1001. if (retval)
  1002. goto class_err;
  1003. return 0;
  1004. class_err:
  1005. #ifdef CONFIG_I2C_COMPAT
  1006. class_compat_unregister(i2c_adapter_compat_class);
  1007. bus_err:
  1008. #endif
  1009. bus_unregister(&i2c_bus_type);
  1010. return retval;
  1011. }
  1012. static void __exit i2c_exit(void)
  1013. {
  1014. i2c_del_driver(&dummy_driver);
  1015. #ifdef CONFIG_I2C_COMPAT
  1016. class_compat_unregister(i2c_adapter_compat_class);
  1017. #endif
  1018. bus_unregister(&i2c_bus_type);
  1019. }
  1020. /* We must initialize early, because some subsystems register i2c drivers
  1021. * in subsys_initcall() code, but are linked (and initialized) before i2c.
  1022. */
  1023. postcore_initcall(i2c_init);
  1024. module_exit(i2c_exit);
  1025. /* ----------------------------------------------------
  1026. * the functional interface to the i2c busses.
  1027. * ----------------------------------------------------
  1028. */
  1029. /**
  1030. * i2c_transfer - execute a single or combined I2C message
  1031. * @adap: Handle to I2C bus
  1032. * @msgs: One or more messages to execute before STOP is issued to
  1033. * terminate the operation; each message begins with a START.
  1034. * @num: Number of messages to be executed.
  1035. *
  1036. * Returns negative errno, else the number of messages executed.
  1037. *
  1038. * Note that there is no requirement that each message be sent to
  1039. * the same slave address, although that is the most common model.
  1040. */
  1041. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1042. {
  1043. unsigned long orig_jiffies;
  1044. int ret, try;
  1045. /* REVISIT the fault reporting model here is weak:
  1046. *
  1047. * - When we get an error after receiving N bytes from a slave,
  1048. * there is no way to report "N".
  1049. *
  1050. * - When we get a NAK after transmitting N bytes to a slave,
  1051. * there is no way to report "N" ... or to let the master
  1052. * continue executing the rest of this combined message, if
  1053. * that's the appropriate response.
  1054. *
  1055. * - When for example "num" is two and we successfully complete
  1056. * the first message but get an error part way through the
  1057. * second, it's unclear whether that should be reported as
  1058. * one (discarding status on the second message) or errno
  1059. * (discarding status on the first one).
  1060. */
  1061. if (adap->algo->master_xfer) {
  1062. #ifdef DEBUG
  1063. for (ret = 0; ret < num; ret++) {
  1064. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  1065. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  1066. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  1067. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  1068. }
  1069. #endif
  1070. if (in_atomic() || irqs_disabled()) {
  1071. ret = i2c_trylock_adapter(adap);
  1072. if (!ret)
  1073. /* I2C activity is ongoing. */
  1074. return -EAGAIN;
  1075. } else {
  1076. i2c_lock_adapter(adap);
  1077. }
  1078. /* Retry automatically on arbitration loss */
  1079. orig_jiffies = jiffies;
  1080. for (ret = 0, try = 0; try <= adap->retries; try++) {
  1081. ret = adap->algo->master_xfer(adap, msgs, num);
  1082. if (ret != -EAGAIN)
  1083. break;
  1084. if (time_after(jiffies, orig_jiffies + adap->timeout))
  1085. break;
  1086. }
  1087. i2c_unlock_adapter(adap);
  1088. return ret;
  1089. } else {
  1090. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  1091. return -EOPNOTSUPP;
  1092. }
  1093. }
  1094. EXPORT_SYMBOL(i2c_transfer);
  1095. /**
  1096. * i2c_master_send - issue a single I2C message in master transmit mode
  1097. * @client: Handle to slave device
  1098. * @buf: Data that will be written to the slave
  1099. * @count: How many bytes to write, must be less than 64k since msg.len is u16
  1100. *
  1101. * Returns negative errno, or else the number of bytes written.
  1102. */
  1103. int i2c_master_send(struct i2c_client *client, const char *buf, int count)
  1104. {
  1105. int ret;
  1106. struct i2c_adapter *adap = client->adapter;
  1107. struct i2c_msg msg;
  1108. msg.addr = client->addr;
  1109. msg.flags = client->flags & I2C_M_TEN;
  1110. msg.len = count;
  1111. msg.buf = (char *)buf;
  1112. ret = i2c_transfer(adap, &msg, 1);
  1113. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  1114. transmitted, else error code. */
  1115. return (ret == 1) ? count : ret;
  1116. }
  1117. EXPORT_SYMBOL(i2c_master_send);
  1118. /**
  1119. * i2c_master_recv - issue a single I2C message in master receive mode
  1120. * @client: Handle to slave device
  1121. * @buf: Where to store data read from slave
  1122. * @count: How many bytes to read, must be less than 64k since msg.len is u16
  1123. *
  1124. * Returns negative errno, or else the number of bytes read.
  1125. */
  1126. int i2c_master_recv(struct i2c_client *client, char *buf, int count)
  1127. {
  1128. struct i2c_adapter *adap = client->adapter;
  1129. struct i2c_msg msg;
  1130. int ret;
  1131. msg.addr = client->addr;
  1132. msg.flags = client->flags & I2C_M_TEN;
  1133. msg.flags |= I2C_M_RD;
  1134. msg.len = count;
  1135. msg.buf = buf;
  1136. ret = i2c_transfer(adap, &msg, 1);
  1137. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  1138. transmitted, else error code. */
  1139. return (ret == 1) ? count : ret;
  1140. }
  1141. EXPORT_SYMBOL(i2c_master_recv);
  1142. /* ----------------------------------------------------
  1143. * the i2c address scanning function
  1144. * Will not work for 10-bit addresses!
  1145. * ----------------------------------------------------
  1146. */
  1147. /*
  1148. * Legacy default probe function, mostly relevant for SMBus. The default
  1149. * probe method is a quick write, but it is known to corrupt the 24RF08
  1150. * EEPROMs due to a state machine bug, and could also irreversibly
  1151. * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
  1152. * we use a short byte read instead. Also, some bus drivers don't implement
  1153. * quick write, so we fallback to a byte read in that case too.
  1154. * On x86, there is another special case for FSC hardware monitoring chips,
  1155. * which want regular byte reads (address 0x73.) Fortunately, these are the
  1156. * only known chips using this I2C address on PC hardware.
  1157. * Returns 1 if probe succeeded, 0 if not.
  1158. */
  1159. static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
  1160. {
  1161. int err;
  1162. union i2c_smbus_data dummy;
  1163. #ifdef CONFIG_X86
  1164. if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
  1165. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  1166. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1167. I2C_SMBUS_BYTE_DATA, &dummy);
  1168. else
  1169. #endif
  1170. if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
  1171. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
  1172. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
  1173. I2C_SMBUS_QUICK, NULL);
  1174. else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
  1175. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1176. I2C_SMBUS_BYTE, &dummy);
  1177. else {
  1178. dev_warn(&adap->dev, "No suitable probing method supported\n");
  1179. err = -EOPNOTSUPP;
  1180. }
  1181. return err >= 0;
  1182. }
  1183. static int i2c_detect_address(struct i2c_client *temp_client,
  1184. struct i2c_driver *driver)
  1185. {
  1186. struct i2c_board_info info;
  1187. struct i2c_adapter *adapter = temp_client->adapter;
  1188. int addr = temp_client->addr;
  1189. int err;
  1190. /* Make sure the address is valid */
  1191. err = i2c_check_addr_validity(addr);
  1192. if (err) {
  1193. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  1194. addr);
  1195. return err;
  1196. }
  1197. /* Skip if already in use */
  1198. if (i2c_check_addr_busy(adapter, addr))
  1199. return 0;
  1200. /* Make sure there is something at this address */
  1201. if (!i2c_default_probe(adapter, addr))
  1202. return 0;
  1203. /* Finally call the custom detection function */
  1204. memset(&info, 0, sizeof(struct i2c_board_info));
  1205. info.addr = addr;
  1206. err = driver->detect(temp_client, &info);
  1207. if (err) {
  1208. /* -ENODEV is returned if the detection fails. We catch it
  1209. here as this isn't an error. */
  1210. return err == -ENODEV ? 0 : err;
  1211. }
  1212. /* Consistency check */
  1213. if (info.type[0] == '\0') {
  1214. dev_err(&adapter->dev, "%s detection function provided "
  1215. "no name for 0x%x\n", driver->driver.name,
  1216. addr);
  1217. } else {
  1218. struct i2c_client *client;
  1219. /* Detection succeeded, instantiate the device */
  1220. dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
  1221. info.type, info.addr);
  1222. client = i2c_new_device(adapter, &info);
  1223. if (client)
  1224. list_add_tail(&client->detected, &driver->clients);
  1225. else
  1226. dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
  1227. info.type, info.addr);
  1228. }
  1229. return 0;
  1230. }
  1231. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
  1232. {
  1233. const unsigned short *address_list;
  1234. struct i2c_client *temp_client;
  1235. int i, err = 0;
  1236. int adap_id = i2c_adapter_id(adapter);
  1237. address_list = driver->address_list;
  1238. if (!driver->detect || !address_list)
  1239. return 0;
  1240. /* Set up a temporary client to help detect callback */
  1241. temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  1242. if (!temp_client)
  1243. return -ENOMEM;
  1244. temp_client->adapter = adapter;
  1245. /* Stop here if the classes do not match */
  1246. if (!(adapter->class & driver->class))
  1247. goto exit_free;
  1248. for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
  1249. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  1250. "addr 0x%02x\n", adap_id, address_list[i]);
  1251. temp_client->addr = address_list[i];
  1252. err = i2c_detect_address(temp_client, driver);
  1253. if (err)
  1254. goto exit_free;
  1255. }
  1256. exit_free:
  1257. kfree(temp_client);
  1258. return err;
  1259. }
  1260. int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
  1261. {
  1262. return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1263. I2C_SMBUS_QUICK, NULL) >= 0;
  1264. }
  1265. EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
  1266. struct i2c_client *
  1267. i2c_new_probed_device(struct i2c_adapter *adap,
  1268. struct i2c_board_info *info,
  1269. unsigned short const *addr_list,
  1270. int (*probe)(struct i2c_adapter *, unsigned short addr))
  1271. {
  1272. int i;
  1273. if (!probe)
  1274. probe = i2c_default_probe;
  1275. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  1276. /* Check address validity */
  1277. if (i2c_check_addr_validity(addr_list[i]) < 0) {
  1278. dev_warn(&adap->dev, "Invalid 7-bit address "
  1279. "0x%02x\n", addr_list[i]);
  1280. continue;
  1281. }
  1282. /* Check address availability */
  1283. if (i2c_check_addr_busy(adap, addr_list[i])) {
  1284. dev_dbg(&adap->dev, "Address 0x%02x already in "
  1285. "use, not probing\n", addr_list[i]);
  1286. continue;
  1287. }
  1288. /* Test address responsiveness */
  1289. if (probe(adap, addr_list[i]))
  1290. break;
  1291. }
  1292. if (addr_list[i] == I2C_CLIENT_END) {
  1293. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  1294. return NULL;
  1295. }
  1296. info->addr = addr_list[i];
  1297. return i2c_new_device(adap, info);
  1298. }
  1299. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  1300. struct i2c_adapter *i2c_get_adapter(int id)
  1301. {
  1302. struct i2c_adapter *adapter;
  1303. mutex_lock(&core_lock);
  1304. adapter = idr_find(&i2c_adapter_idr, id);
  1305. if (adapter && !try_module_get(adapter->owner))
  1306. adapter = NULL;
  1307. mutex_unlock(&core_lock);
  1308. return adapter;
  1309. }
  1310. EXPORT_SYMBOL(i2c_get_adapter);
  1311. void i2c_put_adapter(struct i2c_adapter *adap)
  1312. {
  1313. module_put(adap->owner);
  1314. }
  1315. EXPORT_SYMBOL(i2c_put_adapter);
  1316. /* The SMBus parts */
  1317. #define POLY (0x1070U << 3)
  1318. static u8 crc8(u16 data)
  1319. {
  1320. int i;
  1321. for (i = 0; i < 8; i++) {
  1322. if (data & 0x8000)
  1323. data = data ^ POLY;
  1324. data = data << 1;
  1325. }
  1326. return (u8)(data >> 8);
  1327. }
  1328. /* Incremental CRC8 over count bytes in the array pointed to by p */
  1329. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  1330. {
  1331. int i;
  1332. for (i = 0; i < count; i++)
  1333. crc = crc8((crc ^ p[i]) << 8);
  1334. return crc;
  1335. }
  1336. /* Assume a 7-bit address, which is reasonable for SMBus */
  1337. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  1338. {
  1339. /* The address will be sent first */
  1340. u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
  1341. pec = i2c_smbus_pec(pec, &addr, 1);
  1342. /* The data buffer follows */
  1343. return i2c_smbus_pec(pec, msg->buf, msg->len);
  1344. }
  1345. /* Used for write only transactions */
  1346. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  1347. {
  1348. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  1349. msg->len++;
  1350. }
  1351. /* Return <0 on CRC error
  1352. If there was a write before this read (most cases) we need to take the
  1353. partial CRC from the write part into account.
  1354. Note that this function does modify the message (we need to decrease the
  1355. message length to hide the CRC byte from the caller). */
  1356. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  1357. {
  1358. u8 rpec = msg->buf[--msg->len];
  1359. cpec = i2c_smbus_msg_pec(cpec, msg);
  1360. if (rpec != cpec) {
  1361. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  1362. rpec, cpec);
  1363. return -EBADMSG;
  1364. }
  1365. return 0;
  1366. }
  1367. /**
  1368. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  1369. * @client: Handle to slave device
  1370. *
  1371. * This executes the SMBus "receive byte" protocol, returning negative errno
  1372. * else the byte received from the device.
  1373. */
  1374. s32 i2c_smbus_read_byte(struct i2c_client *client)
  1375. {
  1376. union i2c_smbus_data data;
  1377. int status;
  1378. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1379. I2C_SMBUS_READ, 0,
  1380. I2C_SMBUS_BYTE, &data);
  1381. return (status < 0) ? status : data.byte;
  1382. }
  1383. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1384. /**
  1385. * i2c_smbus_write_byte - SMBus "send byte" protocol
  1386. * @client: Handle to slave device
  1387. * @value: Byte to be sent
  1388. *
  1389. * This executes the SMBus "send byte" protocol, returning negative errno
  1390. * else zero on success.
  1391. */
  1392. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  1393. {
  1394. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1395. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  1396. }
  1397. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1398. /**
  1399. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  1400. * @client: Handle to slave device
  1401. * @command: Byte interpreted by slave
  1402. *
  1403. * This executes the SMBus "read byte" protocol, returning negative errno
  1404. * else a data byte received from the device.
  1405. */
  1406. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  1407. {
  1408. union i2c_smbus_data data;
  1409. int status;
  1410. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1411. I2C_SMBUS_READ, command,
  1412. I2C_SMBUS_BYTE_DATA, &data);
  1413. return (status < 0) ? status : data.byte;
  1414. }
  1415. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1416. /**
  1417. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  1418. * @client: Handle to slave device
  1419. * @command: Byte interpreted by slave
  1420. * @value: Byte being written
  1421. *
  1422. * This executes the SMBus "write byte" protocol, returning negative errno
  1423. * else zero on success.
  1424. */
  1425. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  1426. {
  1427. union i2c_smbus_data data;
  1428. data.byte = value;
  1429. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1430. I2C_SMBUS_WRITE, command,
  1431. I2C_SMBUS_BYTE_DATA, &data);
  1432. }
  1433. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1434. /**
  1435. * i2c_smbus_read_word_data - SMBus "read word" protocol
  1436. * @client: Handle to slave device
  1437. * @command: Byte interpreted by slave
  1438. *
  1439. * This executes the SMBus "read word" protocol, returning negative errno
  1440. * else a 16-bit unsigned "word" received from the device.
  1441. */
  1442. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  1443. {
  1444. union i2c_smbus_data data;
  1445. int status;
  1446. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1447. I2C_SMBUS_READ, command,
  1448. I2C_SMBUS_WORD_DATA, &data);
  1449. return (status < 0) ? status : data.word;
  1450. }
  1451. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1452. /**
  1453. * i2c_smbus_write_word_data - SMBus "write word" protocol
  1454. * @client: Handle to slave device
  1455. * @command: Byte interpreted by slave
  1456. * @value: 16-bit "word" being written
  1457. *
  1458. * This executes the SMBus "write word" protocol, returning negative errno
  1459. * else zero on success.
  1460. */
  1461. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  1462. {
  1463. union i2c_smbus_data data;
  1464. data.word = value;
  1465. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1466. I2C_SMBUS_WRITE, command,
  1467. I2C_SMBUS_WORD_DATA, &data);
  1468. }
  1469. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1470. /**
  1471. * i2c_smbus_process_call - SMBus "process call" protocol
  1472. * @client: Handle to slave device
  1473. * @command: Byte interpreted by slave
  1474. * @value: 16-bit "word" being written
  1475. *
  1476. * This executes the SMBus "process call" protocol, returning negative errno
  1477. * else a 16-bit unsigned "word" received from the device.
  1478. */
  1479. s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
  1480. {
  1481. union i2c_smbus_data data;
  1482. int status;
  1483. data.word = value;
  1484. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1485. I2C_SMBUS_WRITE, command,
  1486. I2C_SMBUS_PROC_CALL, &data);
  1487. return (status < 0) ? status : data.word;
  1488. }
  1489. EXPORT_SYMBOL(i2c_smbus_process_call);
  1490. /**
  1491. * i2c_smbus_read_block_data - SMBus "block read" protocol
  1492. * @client: Handle to slave device
  1493. * @command: Byte interpreted by slave
  1494. * @values: Byte array into which data will be read; big enough to hold
  1495. * the data returned by the slave. SMBus allows at most 32 bytes.
  1496. *
  1497. * This executes the SMBus "block read" protocol, returning negative errno
  1498. * else the number of data bytes in the slave's response.
  1499. *
  1500. * Note that using this function requires that the client's adapter support
  1501. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  1502. * support this; its emulation through I2C messaging relies on a specific
  1503. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  1504. */
  1505. s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
  1506. u8 *values)
  1507. {
  1508. union i2c_smbus_data data;
  1509. int status;
  1510. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1511. I2C_SMBUS_READ, command,
  1512. I2C_SMBUS_BLOCK_DATA, &data);
  1513. if (status)
  1514. return status;
  1515. memcpy(values, &data.block[1], data.block[0]);
  1516. return data.block[0];
  1517. }
  1518. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  1519. /**
  1520. * i2c_smbus_write_block_data - SMBus "block write" protocol
  1521. * @client: Handle to slave device
  1522. * @command: Byte interpreted by slave
  1523. * @length: Size of data block; SMBus allows at most 32 bytes
  1524. * @values: Byte array which will be written.
  1525. *
  1526. * This executes the SMBus "block write" protocol, returning negative errno
  1527. * else zero on success.
  1528. */
  1529. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  1530. u8 length, const u8 *values)
  1531. {
  1532. union i2c_smbus_data data;
  1533. if (length > I2C_SMBUS_BLOCK_MAX)
  1534. length = I2C_SMBUS_BLOCK_MAX;
  1535. data.block[0] = length;
  1536. memcpy(&data.block[1], values, length);
  1537. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1538. I2C_SMBUS_WRITE, command,
  1539. I2C_SMBUS_BLOCK_DATA, &data);
  1540. }
  1541. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1542. /* Returns the number of read bytes */
  1543. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
  1544. u8 length, u8 *values)
  1545. {
  1546. union i2c_smbus_data data;
  1547. int status;
  1548. if (length > I2C_SMBUS_BLOCK_MAX)
  1549. length = I2C_SMBUS_BLOCK_MAX;
  1550. data.block[0] = length;
  1551. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1552. I2C_SMBUS_READ, command,
  1553. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1554. if (status < 0)
  1555. return status;
  1556. memcpy(values, &data.block[1], data.block[0]);
  1557. return data.block[0];
  1558. }
  1559. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1560. s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
  1561. u8 length, const u8 *values)
  1562. {
  1563. union i2c_smbus_data data;
  1564. if (length > I2C_SMBUS_BLOCK_MAX)
  1565. length = I2C_SMBUS_BLOCK_MAX;
  1566. data.block[0] = length;
  1567. memcpy(data.block + 1, values, length);
  1568. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1569. I2C_SMBUS_WRITE, command,
  1570. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1571. }
  1572. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  1573. /* Simulate a SMBus command using the i2c protocol
  1574. No checking of parameters is done! */
  1575. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
  1576. unsigned short flags,
  1577. char read_write, u8 command, int size,
  1578. union i2c_smbus_data *data)
  1579. {
  1580. /* So we need to generate a series of msgs. In the case of writing, we
  1581. need to use only one message; when reading, we need two. We initialize
  1582. most things with sane defaults, to keep the code below somewhat
  1583. simpler. */
  1584. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  1585. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  1586. int num = read_write == I2C_SMBUS_READ ? 2 : 1;
  1587. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  1588. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  1589. };
  1590. int i;
  1591. u8 partial_pec = 0;
  1592. int status;
  1593. msgbuf0[0] = command;
  1594. switch (size) {
  1595. case I2C_SMBUS_QUICK:
  1596. msg[0].len = 0;
  1597. /* Special case: The read/write field is used as data */
  1598. msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
  1599. I2C_M_RD : 0);
  1600. num = 1;
  1601. break;
  1602. case I2C_SMBUS_BYTE:
  1603. if (read_write == I2C_SMBUS_READ) {
  1604. /* Special case: only a read! */
  1605. msg[0].flags = I2C_M_RD | flags;
  1606. num = 1;
  1607. }
  1608. break;
  1609. case I2C_SMBUS_BYTE_DATA:
  1610. if (read_write == I2C_SMBUS_READ)
  1611. msg[1].len = 1;
  1612. else {
  1613. msg[0].len = 2;
  1614. msgbuf0[1] = data->byte;
  1615. }
  1616. break;
  1617. case I2C_SMBUS_WORD_DATA:
  1618. if (read_write == I2C_SMBUS_READ)
  1619. msg[1].len = 2;
  1620. else {
  1621. msg[0].len = 3;
  1622. msgbuf0[1] = data->word & 0xff;
  1623. msgbuf0[2] = data->word >> 8;
  1624. }
  1625. break;
  1626. case I2C_SMBUS_PROC_CALL:
  1627. num = 2; /* Special case */
  1628. read_write = I2C_SMBUS_READ;
  1629. msg[0].len = 3;
  1630. msg[1].len = 2;
  1631. msgbuf0[1] = data->word & 0xff;
  1632. msgbuf0[2] = data->word >> 8;
  1633. break;
  1634. case I2C_SMBUS_BLOCK_DATA:
  1635. if (read_write == I2C_SMBUS_READ) {
  1636. msg[1].flags |= I2C_M_RECV_LEN;
  1637. msg[1].len = 1; /* block length will be added by
  1638. the underlying bus driver */
  1639. } else {
  1640. msg[0].len = data->block[0] + 2;
  1641. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  1642. dev_err(&adapter->dev,
  1643. "Invalid block write size %d\n",
  1644. data->block[0]);
  1645. return -EINVAL;
  1646. }
  1647. for (i = 1; i < msg[0].len; i++)
  1648. msgbuf0[i] = data->block[i-1];
  1649. }
  1650. break;
  1651. case I2C_SMBUS_BLOCK_PROC_CALL:
  1652. num = 2; /* Another special case */
  1653. read_write = I2C_SMBUS_READ;
  1654. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  1655. dev_err(&adapter->dev,
  1656. "Invalid block write size %d\n",
  1657. data->block[0]);
  1658. return -EINVAL;
  1659. }
  1660. msg[0].len = data->block[0] + 2;
  1661. for (i = 1; i < msg[0].len; i++)
  1662. msgbuf0[i] = data->block[i-1];
  1663. msg[1].flags |= I2C_M_RECV_LEN;
  1664. msg[1].len = 1; /* block length will be added by
  1665. the underlying bus driver */
  1666. break;
  1667. case I2C_SMBUS_I2C_BLOCK_DATA:
  1668. if (read_write == I2C_SMBUS_READ) {
  1669. msg[1].len = data->block[0];
  1670. } else {
  1671. msg[0].len = data->block[0] + 1;
  1672. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  1673. dev_err(&adapter->dev,
  1674. "Invalid block write size %d\n",
  1675. data->block[0]);
  1676. return -EINVAL;
  1677. }
  1678. for (i = 1; i <= data->block[0]; i++)
  1679. msgbuf0[i] = data->block[i];
  1680. }
  1681. break;
  1682. default:
  1683. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  1684. return -EOPNOTSUPP;
  1685. }
  1686. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  1687. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  1688. if (i) {
  1689. /* Compute PEC if first message is a write */
  1690. if (!(msg[0].flags & I2C_M_RD)) {
  1691. if (num == 1) /* Write only */
  1692. i2c_smbus_add_pec(&msg[0]);
  1693. else /* Write followed by read */
  1694. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  1695. }
  1696. /* Ask for PEC if last message is a read */
  1697. if (msg[num-1].flags & I2C_M_RD)
  1698. msg[num-1].len++;
  1699. }
  1700. status = i2c_transfer(adapter, msg, num);
  1701. if (status < 0)
  1702. return status;
  1703. /* Check PEC if last message is a read */
  1704. if (i && (msg[num-1].flags & I2C_M_RD)) {
  1705. status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
  1706. if (status < 0)
  1707. return status;
  1708. }
  1709. if (read_write == I2C_SMBUS_READ)
  1710. switch (size) {
  1711. case I2C_SMBUS_BYTE:
  1712. data->byte = msgbuf0[0];
  1713. break;
  1714. case I2C_SMBUS_BYTE_DATA:
  1715. data->byte = msgbuf1[0];
  1716. break;
  1717. case I2C_SMBUS_WORD_DATA:
  1718. case I2C_SMBUS_PROC_CALL:
  1719. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  1720. break;
  1721. case I2C_SMBUS_I2C_BLOCK_DATA:
  1722. for (i = 0; i < data->block[0]; i++)
  1723. data->block[i+1] = msgbuf1[i];
  1724. break;
  1725. case I2C_SMBUS_BLOCK_DATA:
  1726. case I2C_SMBUS_BLOCK_PROC_CALL:
  1727. for (i = 0; i < msgbuf1[0] + 1; i++)
  1728. data->block[i] = msgbuf1[i];
  1729. break;
  1730. }
  1731. return 0;
  1732. }
  1733. /**
  1734. * i2c_smbus_xfer - execute SMBus protocol operations
  1735. * @adapter: Handle to I2C bus
  1736. * @addr: Address of SMBus slave on that bus
  1737. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  1738. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  1739. * @command: Byte interpreted by slave, for protocols which use such bytes
  1740. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  1741. * @data: Data to be read or written
  1742. *
  1743. * This executes an SMBus protocol operation, and returns a negative
  1744. * errno code else zero on success.
  1745. */
  1746. s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
  1747. char read_write, u8 command, int protocol,
  1748. union i2c_smbus_data *data)
  1749. {
  1750. unsigned long orig_jiffies;
  1751. int try;
  1752. s32 res;
  1753. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  1754. if (adapter->algo->smbus_xfer) {
  1755. i2c_lock_adapter(adapter);
  1756. /* Retry automatically on arbitration loss */
  1757. orig_jiffies = jiffies;
  1758. for (res = 0, try = 0; try <= adapter->retries; try++) {
  1759. res = adapter->algo->smbus_xfer(adapter, addr, flags,
  1760. read_write, command,
  1761. protocol, data);
  1762. if (res != -EAGAIN)
  1763. break;
  1764. if (time_after(jiffies,
  1765. orig_jiffies + adapter->timeout))
  1766. break;
  1767. }
  1768. i2c_unlock_adapter(adapter);
  1769. } else
  1770. res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
  1771. command, protocol, data);
  1772. return res;
  1773. }
  1774. EXPORT_SYMBOL(i2c_smbus_xfer);
  1775. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1776. MODULE_DESCRIPTION("I2C-Bus main module");
  1777. MODULE_LICENSE("GPL");