i2c-core.c 58 KB

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