i2c-core.c 50 KB

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