i2c-core.c 59 KB

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