i2c-core.c 57 KB

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