i2c-core.c 55 KB

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