i2c-core-base.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux I2C core
  4. *
  5. * Copyright (C) 1995-99 Simon G. Vogl
  6. * With some changes from Kyösti Mälkki <[email protected]>
  7. * Mux support by Rodolfo Giometti <[email protected]> and
  8. * Michael Lawnick <[email protected]>
  9. *
  10. * Copyright (C) 2013-2017 Wolfram Sang <[email protected]>
  11. */
  12. #define pr_fmt(fmt) "i2c-core: " fmt
  13. #include <dt-bindings/i2c/i2c.h>
  14. #include <linux/acpi.h>
  15. #include <linux/clk/clk-conf.h>
  16. #include <linux/completion.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/errno.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c-smbus.h>
  23. #include <linux/idr.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irqflags.h>
  27. #include <linux/jump_label.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/mutex.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of.h>
  33. #include <linux/of_irq.h>
  34. #include <linux/pinctrl/consumer.h>
  35. #include <linux/pm_domain.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/pm_wakeirq.h>
  38. #include <linux/property.h>
  39. #include <linux/rwsem.h>
  40. #include <linux/slab.h>
  41. #include "i2c-core.h"
  42. #define CREATE_TRACE_POINTS
  43. #include <trace/events/i2c.h>
  44. #define I2C_ADDR_OFFSET_TEN_BIT 0xa000
  45. #define I2C_ADDR_OFFSET_SLAVE 0x1000
  46. #define I2C_ADDR_7BITS_MAX 0x77
  47. #define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
  48. #define I2C_ADDR_DEVICE_ID 0x7c
  49. /*
  50. * core_lock protects i2c_adapter_idr, and guarantees that device detection,
  51. * deletion of detected devices are serialized
  52. */
  53. static DEFINE_MUTEX(core_lock);
  54. static DEFINE_IDR(i2c_adapter_idr);
  55. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
  56. static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key);
  57. static bool is_registered;
  58. int i2c_transfer_trace_reg(void)
  59. {
  60. static_branch_inc(&i2c_trace_msg_key);
  61. return 0;
  62. }
  63. void i2c_transfer_trace_unreg(void)
  64. {
  65. static_branch_dec(&i2c_trace_msg_key);
  66. }
  67. const char *i2c_freq_mode_string(u32 bus_freq_hz)
  68. {
  69. switch (bus_freq_hz) {
  70. case I2C_MAX_STANDARD_MODE_FREQ:
  71. return "Standard Mode (100 kHz)";
  72. case I2C_MAX_FAST_MODE_FREQ:
  73. return "Fast Mode (400 kHz)";
  74. case I2C_MAX_FAST_MODE_PLUS_FREQ:
  75. return "Fast Mode Plus (1.0 MHz)";
  76. case I2C_MAX_TURBO_MODE_FREQ:
  77. return "Turbo Mode (1.4 MHz)";
  78. case I2C_MAX_HIGH_SPEED_MODE_FREQ:
  79. return "High Speed Mode (3.4 MHz)";
  80. case I2C_MAX_ULTRA_FAST_MODE_FREQ:
  81. return "Ultra Fast Mode (5.0 MHz)";
  82. default:
  83. return "Unknown Mode";
  84. }
  85. }
  86. EXPORT_SYMBOL_GPL(i2c_freq_mode_string);
  87. const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  88. const struct i2c_client *client)
  89. {
  90. if (!(id && client))
  91. return NULL;
  92. while (id->name[0]) {
  93. if (strcmp(client->name, id->name) == 0)
  94. return id;
  95. id++;
  96. }
  97. return NULL;
  98. }
  99. EXPORT_SYMBOL_GPL(i2c_match_id);
  100. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  101. {
  102. struct i2c_client *client = i2c_verify_client(dev);
  103. struct i2c_driver *driver;
  104. /* Attempt an OF style match */
  105. if (i2c_of_match_device(drv->of_match_table, client))
  106. return 1;
  107. /* Then ACPI style match */
  108. if (acpi_driver_match_device(dev, drv))
  109. return 1;
  110. driver = to_i2c_driver(drv);
  111. /* Finally an I2C match */
  112. if (i2c_match_id(driver->id_table, client))
  113. return 1;
  114. return 0;
  115. }
  116. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  117. {
  118. struct i2c_client *client = to_i2c_client(dev);
  119. int rc;
  120. rc = of_device_uevent_modalias(dev, env);
  121. if (rc != -ENODEV)
  122. return rc;
  123. rc = acpi_device_uevent_modalias(dev, env);
  124. if (rc != -ENODEV)
  125. return rc;
  126. return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
  127. }
  128. /* i2c bus recovery routines */
  129. static int get_scl_gpio_value(struct i2c_adapter *adap)
  130. {
  131. return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
  132. }
  133. static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
  134. {
  135. gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
  136. }
  137. static int get_sda_gpio_value(struct i2c_adapter *adap)
  138. {
  139. return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
  140. }
  141. static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
  142. {
  143. gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
  144. }
  145. static int i2c_generic_bus_free(struct i2c_adapter *adap)
  146. {
  147. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  148. int ret = -EOPNOTSUPP;
  149. if (bri->get_bus_free)
  150. ret = bri->get_bus_free(adap);
  151. else if (bri->get_sda)
  152. ret = bri->get_sda(adap);
  153. if (ret < 0)
  154. return ret;
  155. return ret ? 0 : -EBUSY;
  156. }
  157. /*
  158. * We are generating clock pulses. ndelay() determines durating of clk pulses.
  159. * We will generate clock with rate 100 KHz and so duration of both clock levels
  160. * is: delay in ns = (10^6 / 100) / 2
  161. */
  162. #define RECOVERY_NDELAY 5000
  163. #define RECOVERY_CLK_CNT 9
  164. int i2c_generic_scl_recovery(struct i2c_adapter *adap)
  165. {
  166. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  167. int i = 0, scl = 1, ret = 0;
  168. if (bri->prepare_recovery)
  169. bri->prepare_recovery(adap);
  170. if (bri->pinctrl)
  171. pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
  172. /*
  173. * If we can set SDA, we will always create a STOP to ensure additional
  174. * pulses will do no harm. This is achieved by letting SDA follow SCL
  175. * half a cycle later. Check the 'incomplete_write_byte' fault injector
  176. * for details. Note that we must honour tsu:sto, 4us, but lets use 5us
  177. * here for simplicity.
  178. */
  179. bri->set_scl(adap, scl);
  180. ndelay(RECOVERY_NDELAY);
  181. if (bri->set_sda)
  182. bri->set_sda(adap, scl);
  183. ndelay(RECOVERY_NDELAY / 2);
  184. /*
  185. * By this time SCL is high, as we need to give 9 falling-rising edges
  186. */
  187. while (i++ < RECOVERY_CLK_CNT * 2) {
  188. if (scl) {
  189. /* SCL shouldn't be low here */
  190. if (!bri->get_scl(adap)) {
  191. dev_err(&adap->dev,
  192. "SCL is stuck low, exit recovery\n");
  193. ret = -EBUSY;
  194. break;
  195. }
  196. }
  197. scl = !scl;
  198. bri->set_scl(adap, scl);
  199. /* Creating STOP again, see above */
  200. if (scl) {
  201. /* Honour minimum tsu:sto */
  202. ndelay(RECOVERY_NDELAY);
  203. } else {
  204. /* Honour minimum tf and thd:dat */
  205. ndelay(RECOVERY_NDELAY / 2);
  206. }
  207. if (bri->set_sda)
  208. bri->set_sda(adap, scl);
  209. ndelay(RECOVERY_NDELAY / 2);
  210. if (scl) {
  211. ret = i2c_generic_bus_free(adap);
  212. if (ret == 0)
  213. break;
  214. }
  215. }
  216. /* If we can't check bus status, assume recovery worked */
  217. if (ret == -EOPNOTSUPP)
  218. ret = 0;
  219. if (bri->unprepare_recovery)
  220. bri->unprepare_recovery(adap);
  221. if (bri->pinctrl)
  222. pinctrl_select_state(bri->pinctrl, bri->pins_default);
  223. return ret;
  224. }
  225. EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
  226. int i2c_recover_bus(struct i2c_adapter *adap)
  227. {
  228. if (!adap->bus_recovery_info)
  229. return -EBUSY;
  230. dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
  231. return adap->bus_recovery_info->recover_bus(adap);
  232. }
  233. EXPORT_SYMBOL_GPL(i2c_recover_bus);
  234. static void i2c_gpio_init_pinctrl_recovery(struct i2c_adapter *adap)
  235. {
  236. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  237. struct device *dev = &adap->dev;
  238. struct pinctrl *p = bri->pinctrl;
  239. /*
  240. * we can't change states without pinctrl, so remove the states if
  241. * populated
  242. */
  243. if (!p) {
  244. bri->pins_default = NULL;
  245. bri->pins_gpio = NULL;
  246. return;
  247. }
  248. if (!bri->pins_default) {
  249. bri->pins_default = pinctrl_lookup_state(p,
  250. PINCTRL_STATE_DEFAULT);
  251. if (IS_ERR(bri->pins_default)) {
  252. dev_dbg(dev, PINCTRL_STATE_DEFAULT " state not found for GPIO recovery\n");
  253. bri->pins_default = NULL;
  254. }
  255. }
  256. if (!bri->pins_gpio) {
  257. bri->pins_gpio = pinctrl_lookup_state(p, "gpio");
  258. if (IS_ERR(bri->pins_gpio))
  259. bri->pins_gpio = pinctrl_lookup_state(p, "recovery");
  260. if (IS_ERR(bri->pins_gpio)) {
  261. dev_dbg(dev, "no gpio or recovery state found for GPIO recovery\n");
  262. bri->pins_gpio = NULL;
  263. }
  264. }
  265. /* for pinctrl state changes, we need all the information */
  266. if (bri->pins_default && bri->pins_gpio) {
  267. dev_info(dev, "using pinctrl states for GPIO recovery");
  268. } else {
  269. bri->pinctrl = NULL;
  270. bri->pins_default = NULL;
  271. bri->pins_gpio = NULL;
  272. }
  273. }
  274. static int i2c_gpio_init_generic_recovery(struct i2c_adapter *adap)
  275. {
  276. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  277. struct device *dev = &adap->dev;
  278. struct gpio_desc *gpiod;
  279. int ret = 0;
  280. /*
  281. * don't touch the recovery information if the driver is not using
  282. * generic SCL recovery
  283. */
  284. if (bri->recover_bus && bri->recover_bus != i2c_generic_scl_recovery)
  285. return 0;
  286. /*
  287. * pins might be taken as GPIO, so we should inform pinctrl about
  288. * this and move the state to GPIO
  289. */
  290. if (bri->pinctrl)
  291. pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
  292. /*
  293. * if there is incomplete or no recovery information, see if generic
  294. * GPIO recovery is available
  295. */
  296. if (!bri->scl_gpiod) {
  297. gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
  298. if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
  299. ret = -EPROBE_DEFER;
  300. goto cleanup_pinctrl_state;
  301. }
  302. if (!IS_ERR(gpiod)) {
  303. bri->scl_gpiod = gpiod;
  304. bri->recover_bus = i2c_generic_scl_recovery;
  305. dev_info(dev, "using generic GPIOs for recovery\n");
  306. }
  307. }
  308. /* SDA GPIOD line is optional, so we care about DEFER only */
  309. if (!bri->sda_gpiod) {
  310. /*
  311. * We have SCL. Pull SCL low and wait a bit so that SDA glitches
  312. * have no effect.
  313. */
  314. gpiod_direction_output(bri->scl_gpiod, 0);
  315. udelay(10);
  316. gpiod = devm_gpiod_get(dev, "sda", GPIOD_IN);
  317. /* Wait a bit in case of a SDA glitch, and then release SCL. */
  318. udelay(10);
  319. gpiod_direction_output(bri->scl_gpiod, 1);
  320. if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
  321. ret = -EPROBE_DEFER;
  322. goto cleanup_pinctrl_state;
  323. }
  324. if (!IS_ERR(gpiod))
  325. bri->sda_gpiod = gpiod;
  326. }
  327. cleanup_pinctrl_state:
  328. /* change the state of the pins back to their default state */
  329. if (bri->pinctrl)
  330. pinctrl_select_state(bri->pinctrl, bri->pins_default);
  331. return ret;
  332. }
  333. static int i2c_gpio_init_recovery(struct i2c_adapter *adap)
  334. {
  335. i2c_gpio_init_pinctrl_recovery(adap);
  336. return i2c_gpio_init_generic_recovery(adap);
  337. }
  338. static int i2c_init_recovery(struct i2c_adapter *adap)
  339. {
  340. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  341. bool is_error_level = true;
  342. char *err_str;
  343. if (!bri)
  344. return 0;
  345. if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER)
  346. return -EPROBE_DEFER;
  347. if (!bri->recover_bus) {
  348. err_str = "no suitable method provided";
  349. is_error_level = false;
  350. goto err;
  351. }
  352. if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
  353. bri->get_scl = get_scl_gpio_value;
  354. bri->set_scl = set_scl_gpio_value;
  355. if (bri->sda_gpiod) {
  356. bri->get_sda = get_sda_gpio_value;
  357. /* FIXME: add proper flag instead of '0' once available */
  358. if (gpiod_get_direction(bri->sda_gpiod) == 0)
  359. bri->set_sda = set_sda_gpio_value;
  360. }
  361. } else if (bri->recover_bus == i2c_generic_scl_recovery) {
  362. /* Generic SCL recovery */
  363. if (!bri->set_scl || !bri->get_scl) {
  364. err_str = "no {get|set}_scl() found";
  365. goto err;
  366. }
  367. if (!bri->set_sda && !bri->get_sda) {
  368. err_str = "either get_sda() or set_sda() needed";
  369. goto err;
  370. }
  371. }
  372. return 0;
  373. err:
  374. if (is_error_level)
  375. dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
  376. else
  377. dev_dbg(&adap->dev, "Not using recovery: %s\n", err_str);
  378. adap->bus_recovery_info = NULL;
  379. return -EINVAL;
  380. }
  381. static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
  382. {
  383. struct i2c_adapter *adap = client->adapter;
  384. unsigned int irq;
  385. if (!adap->host_notify_domain)
  386. return -ENXIO;
  387. if (client->flags & I2C_CLIENT_TEN)
  388. return -EINVAL;
  389. irq = irq_create_mapping(adap->host_notify_domain, client->addr);
  390. return irq > 0 ? irq : -ENXIO;
  391. }
  392. static int i2c_device_probe(struct device *dev)
  393. {
  394. struct i2c_client *client = i2c_verify_client(dev);
  395. struct i2c_driver *driver;
  396. bool do_power_on;
  397. int status;
  398. if (!client)
  399. return 0;
  400. client->irq = client->init_irq;
  401. if (!client->irq) {
  402. int irq = -ENOENT;
  403. if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
  404. dev_dbg(dev, "Using Host Notify IRQ\n");
  405. /* Keep adapter active when Host Notify is required */
  406. pm_runtime_get_sync(&client->adapter->dev);
  407. irq = i2c_smbus_host_notify_to_irq(client);
  408. } else if (dev->of_node) {
  409. irq = of_irq_get_byname(dev->of_node, "irq");
  410. if (irq == -EINVAL || irq == -ENODATA)
  411. irq = of_irq_get(dev->of_node, 0);
  412. } else if (ACPI_COMPANION(dev)) {
  413. bool wake_capable;
  414. irq = i2c_acpi_get_irq(client, &wake_capable);
  415. if (irq > 0 && wake_capable)
  416. client->flags |= I2C_CLIENT_WAKE;
  417. }
  418. if (irq == -EPROBE_DEFER) {
  419. status = irq;
  420. goto put_sync_adapter;
  421. }
  422. if (irq < 0)
  423. irq = 0;
  424. client->irq = irq;
  425. }
  426. driver = to_i2c_driver(dev->driver);
  427. /*
  428. * An I2C ID table is not mandatory, if and only if, a suitable OF
  429. * or ACPI ID table is supplied for the probing device.
  430. */
  431. if (!driver->id_table &&
  432. !acpi_driver_match_device(dev, dev->driver) &&
  433. !i2c_of_match_device(dev->driver->of_match_table, client)) {
  434. status = -ENODEV;
  435. goto put_sync_adapter;
  436. }
  437. if (client->flags & I2C_CLIENT_WAKE) {
  438. int wakeirq;
  439. wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
  440. if (wakeirq == -EPROBE_DEFER) {
  441. status = wakeirq;
  442. goto put_sync_adapter;
  443. }
  444. device_init_wakeup(&client->dev, true);
  445. if (wakeirq > 0 && wakeirq != client->irq)
  446. status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
  447. else if (client->irq > 0)
  448. status = dev_pm_set_wake_irq(dev, client->irq);
  449. else
  450. status = 0;
  451. if (status)
  452. dev_warn(&client->dev, "failed to set up wakeup irq\n");
  453. }
  454. dev_dbg(dev, "probe\n");
  455. status = of_clk_set_defaults(dev->of_node, false);
  456. if (status < 0)
  457. goto err_clear_wakeup_irq;
  458. do_power_on = !i2c_acpi_waive_d0_probe(dev);
  459. status = dev_pm_domain_attach(&client->dev, do_power_on);
  460. if (status)
  461. goto err_clear_wakeup_irq;
  462. client->devres_group_id = devres_open_group(&client->dev, NULL,
  463. GFP_KERNEL);
  464. if (!client->devres_group_id) {
  465. status = -ENOMEM;
  466. goto err_detach_pm_domain;
  467. }
  468. /*
  469. * When there are no more users of probe(),
  470. * rename probe_new to probe.
  471. */
  472. if (driver->probe_new)
  473. status = driver->probe_new(client);
  474. else if (driver->probe)
  475. status = driver->probe(client,
  476. i2c_match_id(driver->id_table, client));
  477. else
  478. status = -EINVAL;
  479. /*
  480. * Note that we are not closing the devres group opened above so
  481. * even resources that were attached to the device after probe is
  482. * run are released when i2c_device_remove() is executed. This is
  483. * needed as some drivers would allocate additional resources,
  484. * for example when updating firmware.
  485. */
  486. if (status)
  487. goto err_release_driver_resources;
  488. return 0;
  489. err_release_driver_resources:
  490. devres_release_group(&client->dev, client->devres_group_id);
  491. err_detach_pm_domain:
  492. dev_pm_domain_detach(&client->dev, do_power_on);
  493. err_clear_wakeup_irq:
  494. dev_pm_clear_wake_irq(&client->dev);
  495. device_init_wakeup(&client->dev, false);
  496. put_sync_adapter:
  497. if (client->flags & I2C_CLIENT_HOST_NOTIFY)
  498. pm_runtime_put_sync(&client->adapter->dev);
  499. return status;
  500. }
  501. static void i2c_device_remove(struct device *dev)
  502. {
  503. struct i2c_client *client = to_i2c_client(dev);
  504. struct i2c_driver *driver;
  505. driver = to_i2c_driver(dev->driver);
  506. if (driver->remove) {
  507. dev_dbg(dev, "remove\n");
  508. driver->remove(client);
  509. }
  510. devres_release_group(&client->dev, client->devres_group_id);
  511. dev_pm_domain_detach(&client->dev, true);
  512. dev_pm_clear_wake_irq(&client->dev);
  513. device_init_wakeup(&client->dev, false);
  514. client->irq = 0;
  515. if (client->flags & I2C_CLIENT_HOST_NOTIFY)
  516. pm_runtime_put(&client->adapter->dev);
  517. }
  518. static void i2c_device_shutdown(struct device *dev)
  519. {
  520. struct i2c_client *client = i2c_verify_client(dev);
  521. struct i2c_driver *driver;
  522. if (!client || !dev->driver)
  523. return;
  524. driver = to_i2c_driver(dev->driver);
  525. if (driver->shutdown)
  526. driver->shutdown(client);
  527. else if (client->irq > 0)
  528. disable_irq(client->irq);
  529. }
  530. static void i2c_client_dev_release(struct device *dev)
  531. {
  532. kfree(to_i2c_client(dev));
  533. }
  534. static ssize_t
  535. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  536. {
  537. return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
  538. to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
  539. }
  540. static DEVICE_ATTR_RO(name);
  541. static ssize_t
  542. modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  543. {
  544. struct i2c_client *client = to_i2c_client(dev);
  545. int len;
  546. len = of_device_modalias(dev, buf, PAGE_SIZE);
  547. if (len != -ENODEV)
  548. return len;
  549. len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
  550. if (len != -ENODEV)
  551. return len;
  552. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  553. }
  554. static DEVICE_ATTR_RO(modalias);
  555. static struct attribute *i2c_dev_attrs[] = {
  556. &dev_attr_name.attr,
  557. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  558. &dev_attr_modalias.attr,
  559. NULL
  560. };
  561. ATTRIBUTE_GROUPS(i2c_dev);
  562. struct bus_type i2c_bus_type = {
  563. .name = "i2c",
  564. .match = i2c_device_match,
  565. .probe = i2c_device_probe,
  566. .remove = i2c_device_remove,
  567. .shutdown = i2c_device_shutdown,
  568. };
  569. EXPORT_SYMBOL_GPL(i2c_bus_type);
  570. struct device_type i2c_client_type = {
  571. .groups = i2c_dev_groups,
  572. .uevent = i2c_device_uevent,
  573. .release = i2c_client_dev_release,
  574. };
  575. EXPORT_SYMBOL_GPL(i2c_client_type);
  576. /**
  577. * i2c_verify_client - return parameter as i2c_client, or NULL
  578. * @dev: device, probably from some driver model iterator
  579. *
  580. * When traversing the driver model tree, perhaps using driver model
  581. * iterators like @device_for_each_child(), you can't assume very much
  582. * about the nodes you find. Use this function to avoid oopses caused
  583. * by wrongly treating some non-I2C device as an i2c_client.
  584. */
  585. struct i2c_client *i2c_verify_client(struct device *dev)
  586. {
  587. return (dev->type == &i2c_client_type)
  588. ? to_i2c_client(dev)
  589. : NULL;
  590. }
  591. EXPORT_SYMBOL(i2c_verify_client);
  592. /* Return a unique address which takes the flags of the client into account */
  593. static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
  594. {
  595. unsigned short addr = client->addr;
  596. /* For some client flags, add an arbitrary offset to avoid collisions */
  597. if (client->flags & I2C_CLIENT_TEN)
  598. addr |= I2C_ADDR_OFFSET_TEN_BIT;
  599. if (client->flags & I2C_CLIENT_SLAVE)
  600. addr |= I2C_ADDR_OFFSET_SLAVE;
  601. return addr;
  602. }
  603. /* This is a permissive address validity check, I2C address map constraints
  604. * are purposely not enforced, except for the general call address. */
  605. static int i2c_check_addr_validity(unsigned int addr, unsigned short flags)
  606. {
  607. if (flags & I2C_CLIENT_TEN) {
  608. /* 10-bit address, all values are valid */
  609. if (addr > 0x3ff)
  610. return -EINVAL;
  611. } else {
  612. /* 7-bit address, reject the general call address */
  613. if (addr == 0x00 || addr > 0x7f)
  614. return -EINVAL;
  615. }
  616. return 0;
  617. }
  618. /* And this is a strict address validity check, used when probing. If a
  619. * device uses a reserved address, then it shouldn't be probed. 7-bit
  620. * addressing is assumed, 10-bit address devices are rare and should be
  621. * explicitly enumerated. */
  622. int i2c_check_7bit_addr_validity_strict(unsigned short addr)
  623. {
  624. /*
  625. * Reserved addresses per I2C specification:
  626. * 0x00 General call address / START byte
  627. * 0x01 CBUS address
  628. * 0x02 Reserved for different bus format
  629. * 0x03 Reserved for future purposes
  630. * 0x04-0x07 Hs-mode master code
  631. * 0x78-0x7b 10-bit slave addressing
  632. * 0x7c-0x7f Reserved for future purposes
  633. */
  634. if (addr < 0x08 || addr > 0x77)
  635. return -EINVAL;
  636. return 0;
  637. }
  638. static int __i2c_check_addr_busy(struct device *dev, void *addrp)
  639. {
  640. struct i2c_client *client = i2c_verify_client(dev);
  641. int addr = *(int *)addrp;
  642. if (client && i2c_encode_flags_to_addr(client) == addr)
  643. return -EBUSY;
  644. return 0;
  645. }
  646. /* walk up mux tree */
  647. static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
  648. {
  649. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  650. int result;
  651. result = device_for_each_child(&adapter->dev, &addr,
  652. __i2c_check_addr_busy);
  653. if (!result && parent)
  654. result = i2c_check_mux_parents(parent, addr);
  655. return result;
  656. }
  657. /* recurse down mux tree */
  658. static int i2c_check_mux_children(struct device *dev, void *addrp)
  659. {
  660. int result;
  661. if (dev->type == &i2c_adapter_type)
  662. result = device_for_each_child(dev, addrp,
  663. i2c_check_mux_children);
  664. else
  665. result = __i2c_check_addr_busy(dev, addrp);
  666. return result;
  667. }
  668. static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
  669. {
  670. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  671. int result = 0;
  672. if (parent)
  673. result = i2c_check_mux_parents(parent, addr);
  674. if (!result)
  675. result = device_for_each_child(&adapter->dev, &addr,
  676. i2c_check_mux_children);
  677. return result;
  678. }
  679. /**
  680. * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
  681. * @adapter: Target I2C bus segment
  682. * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
  683. * locks only this branch in the adapter tree
  684. */
  685. static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
  686. unsigned int flags)
  687. {
  688. rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
  689. }
  690. /**
  691. * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
  692. * @adapter: Target I2C bus segment
  693. * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
  694. * trylocks only this branch in the adapter tree
  695. */
  696. static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
  697. unsigned int flags)
  698. {
  699. return rt_mutex_trylock(&adapter->bus_lock);
  700. }
  701. /**
  702. * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
  703. * @adapter: Target I2C bus segment
  704. * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
  705. * unlocks only this branch in the adapter tree
  706. */
  707. static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
  708. unsigned int flags)
  709. {
  710. rt_mutex_unlock(&adapter->bus_lock);
  711. }
  712. static void i2c_dev_set_name(struct i2c_adapter *adap,
  713. struct i2c_client *client,
  714. struct i2c_board_info const *info)
  715. {
  716. struct acpi_device *adev = ACPI_COMPANION(&client->dev);
  717. if (info && info->dev_name) {
  718. dev_set_name(&client->dev, "i2c-%s", info->dev_name);
  719. return;
  720. }
  721. if (adev) {
  722. dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
  723. return;
  724. }
  725. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
  726. i2c_encode_flags_to_addr(client));
  727. }
  728. int i2c_dev_irq_from_resources(const struct resource *resources,
  729. unsigned int num_resources)
  730. {
  731. struct irq_data *irqd;
  732. int i;
  733. for (i = 0; i < num_resources; i++) {
  734. const struct resource *r = &resources[i];
  735. if (resource_type(r) != IORESOURCE_IRQ)
  736. continue;
  737. if (r->flags & IORESOURCE_BITS) {
  738. irqd = irq_get_irq_data(r->start);
  739. if (!irqd)
  740. break;
  741. irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
  742. }
  743. return r->start;
  744. }
  745. return 0;
  746. }
  747. /**
  748. * i2c_new_client_device - instantiate an i2c device
  749. * @adap: the adapter managing the device
  750. * @info: describes one I2C device; bus_num is ignored
  751. * Context: can sleep
  752. *
  753. * Create an i2c device. Binding is handled through driver model
  754. * probe()/remove() methods. A driver may be bound to this device when we
  755. * return from this function, or any later moment (e.g. maybe hotplugging will
  756. * load the driver module). This call is not appropriate for use by mainboard
  757. * initialization logic, which usually runs during an arch_initcall() long
  758. * before any i2c_adapter could exist.
  759. *
  760. * This returns the new i2c client, which may be saved for later use with
  761. * i2c_unregister_device(); or an ERR_PTR to describe the error.
  762. */
  763. struct i2c_client *
  764. i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  765. {
  766. struct i2c_client *client;
  767. bool need_put = false;
  768. int status;
  769. client = kzalloc(sizeof *client, GFP_KERNEL);
  770. if (!client)
  771. return ERR_PTR(-ENOMEM);
  772. client->adapter = adap;
  773. client->dev.platform_data = info->platform_data;
  774. client->flags = info->flags;
  775. client->addr = info->addr;
  776. client->init_irq = info->irq;
  777. if (!client->init_irq)
  778. client->init_irq = i2c_dev_irq_from_resources(info->resources,
  779. info->num_resources);
  780. strscpy(client->name, info->type, sizeof(client->name));
  781. status = i2c_check_addr_validity(client->addr, client->flags);
  782. if (status) {
  783. dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
  784. client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
  785. goto out_err_silent;
  786. }
  787. /* Check for address business */
  788. status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
  789. if (status)
  790. goto out_err;
  791. client->dev.parent = &client->adapter->dev;
  792. client->dev.bus = &i2c_bus_type;
  793. client->dev.type = &i2c_client_type;
  794. client->dev.of_node = of_node_get(info->of_node);
  795. client->dev.fwnode = info->fwnode;
  796. device_enable_async_suspend(&client->dev);
  797. if (info->swnode) {
  798. status = device_add_software_node(&client->dev, info->swnode);
  799. if (status) {
  800. dev_err(&adap->dev,
  801. "Failed to add software node to client %s: %d\n",
  802. client->name, status);
  803. goto out_err_put_of_node;
  804. }
  805. }
  806. i2c_dev_set_name(adap, client, info);
  807. status = device_register(&client->dev);
  808. if (status)
  809. goto out_remove_swnode;
  810. dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  811. client->name, dev_name(&client->dev));
  812. return client;
  813. out_remove_swnode:
  814. device_remove_software_node(&client->dev);
  815. need_put = true;
  816. out_err_put_of_node:
  817. of_node_put(info->of_node);
  818. out_err:
  819. dev_err(&adap->dev,
  820. "Failed to register i2c client %s at 0x%02x (%d)\n",
  821. client->name, client->addr, status);
  822. out_err_silent:
  823. if (need_put)
  824. put_device(&client->dev);
  825. else
  826. kfree(client);
  827. return ERR_PTR(status);
  828. }
  829. EXPORT_SYMBOL_GPL(i2c_new_client_device);
  830. /**
  831. * i2c_unregister_device - reverse effect of i2c_new_*_device()
  832. * @client: value returned from i2c_new_*_device()
  833. * Context: can sleep
  834. */
  835. void i2c_unregister_device(struct i2c_client *client)
  836. {
  837. if (IS_ERR_OR_NULL(client))
  838. return;
  839. if (client->dev.of_node) {
  840. of_node_clear_flag(client->dev.of_node, OF_POPULATED);
  841. of_node_put(client->dev.of_node);
  842. }
  843. if (ACPI_COMPANION(&client->dev))
  844. acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
  845. device_remove_software_node(&client->dev);
  846. device_unregister(&client->dev);
  847. }
  848. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  849. static const struct i2c_device_id dummy_id[] = {
  850. { "dummy", 0 },
  851. { },
  852. };
  853. static int dummy_probe(struct i2c_client *client,
  854. const struct i2c_device_id *id)
  855. {
  856. return 0;
  857. }
  858. static struct i2c_driver dummy_driver = {
  859. .driver.name = "dummy",
  860. .probe = dummy_probe,
  861. .id_table = dummy_id,
  862. };
  863. /**
  864. * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
  865. * @adapter: the adapter managing the device
  866. * @address: seven bit address to be used
  867. * Context: can sleep
  868. *
  869. * This returns an I2C client bound to the "dummy" driver, intended for use
  870. * with devices that consume multiple addresses. Examples of such chips
  871. * include various EEPROMS (like 24c04 and 24c08 models).
  872. *
  873. * These dummy devices have two main uses. First, most I2C and SMBus calls
  874. * except i2c_transfer() need a client handle; the dummy will be that handle.
  875. * And second, this prevents the specified address from being bound to a
  876. * different driver.
  877. *
  878. * This returns the new i2c client, which should be saved for later use with
  879. * i2c_unregister_device(); or an ERR_PTR to describe the error.
  880. */
  881. struct i2c_client *i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address)
  882. {
  883. struct i2c_board_info info = {
  884. I2C_BOARD_INFO("dummy", address),
  885. };
  886. return i2c_new_client_device(adapter, &info);
  887. }
  888. EXPORT_SYMBOL_GPL(i2c_new_dummy_device);
  889. static void devm_i2c_release_dummy(void *client)
  890. {
  891. i2c_unregister_device(client);
  892. }
  893. /**
  894. * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
  895. * @dev: device the managed resource is bound to
  896. * @adapter: the adapter managing the device
  897. * @address: seven bit address to be used
  898. * Context: can sleep
  899. *
  900. * This is the device-managed version of @i2c_new_dummy_device. It returns the
  901. * new i2c client or an ERR_PTR in case of an error.
  902. */
  903. struct i2c_client *devm_i2c_new_dummy_device(struct device *dev,
  904. struct i2c_adapter *adapter,
  905. u16 address)
  906. {
  907. struct i2c_client *client;
  908. int ret;
  909. client = i2c_new_dummy_device(adapter, address);
  910. if (IS_ERR(client))
  911. return client;
  912. ret = devm_add_action_or_reset(dev, devm_i2c_release_dummy, client);
  913. if (ret)
  914. return ERR_PTR(ret);
  915. return client;
  916. }
  917. EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);
  918. /**
  919. * i2c_new_ancillary_device - Helper to get the instantiated secondary address
  920. * and create the associated device
  921. * @client: Handle to the primary client
  922. * @name: Handle to specify which secondary address to get
  923. * @default_addr: Used as a fallback if no secondary address was specified
  924. * Context: can sleep
  925. *
  926. * I2C clients can be composed of multiple I2C slaves bound together in a single
  927. * component. The I2C client driver then binds to the master I2C slave and needs
  928. * to create I2C dummy clients to communicate with all the other slaves.
  929. *
  930. * This function creates and returns an I2C dummy client whose I2C address is
  931. * retrieved from the platform firmware based on the given slave name. If no
  932. * address is specified by the firmware default_addr is used.
  933. *
  934. * On DT-based platforms the address is retrieved from the "reg" property entry
  935. * cell whose "reg-names" value matches the slave name.
  936. *
  937. * This returns the new i2c client, which should be saved for later use with
  938. * i2c_unregister_device(); or an ERR_PTR to describe the error.
  939. */
  940. struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
  941. const char *name,
  942. u16 default_addr)
  943. {
  944. struct device_node *np = client->dev.of_node;
  945. u32 addr = default_addr;
  946. int i;
  947. if (np) {
  948. i = of_property_match_string(np, "reg-names", name);
  949. if (i >= 0)
  950. of_property_read_u32_index(np, "reg", i, &addr);
  951. }
  952. dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
  953. return i2c_new_dummy_device(client->adapter, addr);
  954. }
  955. EXPORT_SYMBOL_GPL(i2c_new_ancillary_device);
  956. /* ------------------------------------------------------------------------- */
  957. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  958. static void i2c_adapter_dev_release(struct device *dev)
  959. {
  960. struct i2c_adapter *adap = to_i2c_adapter(dev);
  961. complete(&adap->dev_released);
  962. }
  963. unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
  964. {
  965. unsigned int depth = 0;
  966. while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
  967. depth++;
  968. WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
  969. "adapter depth exceeds lockdep subclass limit\n");
  970. return depth;
  971. }
  972. EXPORT_SYMBOL_GPL(i2c_adapter_depth);
  973. /*
  974. * Let users instantiate I2C devices through sysfs. This can be used when
  975. * platform initialization code doesn't contain the proper data for
  976. * whatever reason. Also useful for drivers that do device detection and
  977. * detection fails, either because the device uses an unexpected address,
  978. * or this is a compatible device with different ID register values.
  979. *
  980. * Parameter checking may look overzealous, but we really don't want
  981. * the user to provide incorrect parameters.
  982. */
  983. static ssize_t
  984. new_device_store(struct device *dev, struct device_attribute *attr,
  985. const char *buf, size_t count)
  986. {
  987. struct i2c_adapter *adap = to_i2c_adapter(dev);
  988. struct i2c_board_info info;
  989. struct i2c_client *client;
  990. char *blank, end;
  991. int res;
  992. memset(&info, 0, sizeof(struct i2c_board_info));
  993. blank = strchr(buf, ' ');
  994. if (!blank) {
  995. dev_err(dev, "%s: Missing parameters\n", "new_device");
  996. return -EINVAL;
  997. }
  998. if (blank - buf > I2C_NAME_SIZE - 1) {
  999. dev_err(dev, "%s: Invalid device name\n", "new_device");
  1000. return -EINVAL;
  1001. }
  1002. memcpy(info.type, buf, blank - buf);
  1003. /* Parse remaining parameters, reject extra parameters */
  1004. res = sscanf(++blank, "%hi%c", &info.addr, &end);
  1005. if (res < 1) {
  1006. dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
  1007. return -EINVAL;
  1008. }
  1009. if (res > 1 && end != '\n') {
  1010. dev_err(dev, "%s: Extra parameters\n", "new_device");
  1011. return -EINVAL;
  1012. }
  1013. if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
  1014. info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
  1015. info.flags |= I2C_CLIENT_TEN;
  1016. }
  1017. if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
  1018. info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
  1019. info.flags |= I2C_CLIENT_SLAVE;
  1020. }
  1021. client = i2c_new_client_device(adap, &info);
  1022. if (IS_ERR(client))
  1023. return PTR_ERR(client);
  1024. /* Keep track of the added device */
  1025. mutex_lock(&adap->userspace_clients_lock);
  1026. list_add_tail(&client->detected, &adap->userspace_clients);
  1027. mutex_unlock(&adap->userspace_clients_lock);
  1028. dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
  1029. info.type, info.addr);
  1030. return count;
  1031. }
  1032. static DEVICE_ATTR_WO(new_device);
  1033. /*
  1034. * And of course let the users delete the devices they instantiated, if
  1035. * they got it wrong. This interface can only be used to delete devices
  1036. * instantiated by i2c_sysfs_new_device above. This guarantees that we
  1037. * don't delete devices to which some kernel code still has references.
  1038. *
  1039. * Parameter checking may look overzealous, but we really don't want
  1040. * the user to delete the wrong device.
  1041. */
  1042. static ssize_t
  1043. delete_device_store(struct device *dev, struct device_attribute *attr,
  1044. const char *buf, size_t count)
  1045. {
  1046. struct i2c_adapter *adap = to_i2c_adapter(dev);
  1047. struct i2c_client *client, *next;
  1048. unsigned short addr;
  1049. char end;
  1050. int res;
  1051. /* Parse parameters, reject extra parameters */
  1052. res = sscanf(buf, "%hi%c", &addr, &end);
  1053. if (res < 1) {
  1054. dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
  1055. return -EINVAL;
  1056. }
  1057. if (res > 1 && end != '\n') {
  1058. dev_err(dev, "%s: Extra parameters\n", "delete_device");
  1059. return -EINVAL;
  1060. }
  1061. /* Make sure the device was added through sysfs */
  1062. res = -ENOENT;
  1063. mutex_lock_nested(&adap->userspace_clients_lock,
  1064. i2c_adapter_depth(adap));
  1065. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  1066. detected) {
  1067. if (i2c_encode_flags_to_addr(client) == addr) {
  1068. dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
  1069. "delete_device", client->name, client->addr);
  1070. list_del(&client->detected);
  1071. i2c_unregister_device(client);
  1072. res = count;
  1073. break;
  1074. }
  1075. }
  1076. mutex_unlock(&adap->userspace_clients_lock);
  1077. if (res < 0)
  1078. dev_err(dev, "%s: Can't find device in list\n",
  1079. "delete_device");
  1080. return res;
  1081. }
  1082. static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
  1083. delete_device_store);
  1084. static struct attribute *i2c_adapter_attrs[] = {
  1085. &dev_attr_name.attr,
  1086. &dev_attr_new_device.attr,
  1087. &dev_attr_delete_device.attr,
  1088. NULL
  1089. };
  1090. ATTRIBUTE_GROUPS(i2c_adapter);
  1091. struct device_type i2c_adapter_type = {
  1092. .groups = i2c_adapter_groups,
  1093. .release = i2c_adapter_dev_release,
  1094. };
  1095. EXPORT_SYMBOL_GPL(i2c_adapter_type);
  1096. /**
  1097. * i2c_verify_adapter - return parameter as i2c_adapter or NULL
  1098. * @dev: device, probably from some driver model iterator
  1099. *
  1100. * When traversing the driver model tree, perhaps using driver model
  1101. * iterators like @device_for_each_child(), you can't assume very much
  1102. * about the nodes you find. Use this function to avoid oopses caused
  1103. * by wrongly treating some non-I2C device as an i2c_adapter.
  1104. */
  1105. struct i2c_adapter *i2c_verify_adapter(struct device *dev)
  1106. {
  1107. return (dev->type == &i2c_adapter_type)
  1108. ? to_i2c_adapter(dev)
  1109. : NULL;
  1110. }
  1111. EXPORT_SYMBOL(i2c_verify_adapter);
  1112. #ifdef CONFIG_I2C_COMPAT
  1113. static struct class_compat *i2c_adapter_compat_class;
  1114. #endif
  1115. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  1116. {
  1117. struct i2c_devinfo *devinfo;
  1118. down_read(&__i2c_board_lock);
  1119. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  1120. if (devinfo->busnum == adapter->nr &&
  1121. IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info)))
  1122. dev_err(&adapter->dev,
  1123. "Can't create device at 0x%02x\n",
  1124. devinfo->board_info.addr);
  1125. }
  1126. up_read(&__i2c_board_lock);
  1127. }
  1128. static int i2c_do_add_adapter(struct i2c_driver *driver,
  1129. struct i2c_adapter *adap)
  1130. {
  1131. /* Detect supported devices on that bus, and instantiate them */
  1132. i2c_detect(adap, driver);
  1133. return 0;
  1134. }
  1135. static int __process_new_adapter(struct device_driver *d, void *data)
  1136. {
  1137. return i2c_do_add_adapter(to_i2c_driver(d), data);
  1138. }
  1139. static const struct i2c_lock_operations i2c_adapter_lock_ops = {
  1140. .lock_bus = i2c_adapter_lock_bus,
  1141. .trylock_bus = i2c_adapter_trylock_bus,
  1142. .unlock_bus = i2c_adapter_unlock_bus,
  1143. };
  1144. static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
  1145. {
  1146. struct irq_domain *domain = adap->host_notify_domain;
  1147. irq_hw_number_t hwirq;
  1148. if (!domain)
  1149. return;
  1150. for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
  1151. irq_dispose_mapping(irq_find_mapping(domain, hwirq));
  1152. irq_domain_remove(domain);
  1153. adap->host_notify_domain = NULL;
  1154. }
  1155. static int i2c_host_notify_irq_map(struct irq_domain *h,
  1156. unsigned int virq,
  1157. irq_hw_number_t hw_irq_num)
  1158. {
  1159. irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
  1160. return 0;
  1161. }
  1162. static const struct irq_domain_ops i2c_host_notify_irq_ops = {
  1163. .map = i2c_host_notify_irq_map,
  1164. };
  1165. static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
  1166. {
  1167. struct irq_domain *domain;
  1168. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
  1169. return 0;
  1170. domain = irq_domain_create_linear(adap->dev.parent->fwnode,
  1171. I2C_ADDR_7BITS_COUNT,
  1172. &i2c_host_notify_irq_ops, adap);
  1173. if (!domain)
  1174. return -ENOMEM;
  1175. adap->host_notify_domain = domain;
  1176. return 0;
  1177. }
  1178. /**
  1179. * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
  1180. * I2C client.
  1181. * @adap: the adapter
  1182. * @addr: the I2C address of the notifying device
  1183. * Context: can't sleep
  1184. *
  1185. * Helper function to be called from an I2C bus driver's interrupt
  1186. * handler. It will schedule the Host Notify IRQ.
  1187. */
  1188. int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
  1189. {
  1190. int irq;
  1191. if (!adap)
  1192. return -EINVAL;
  1193. irq = irq_find_mapping(adap->host_notify_domain, addr);
  1194. if (irq <= 0)
  1195. return -ENXIO;
  1196. generic_handle_irq_safe(irq);
  1197. return 0;
  1198. }
  1199. EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
  1200. static int i2c_register_adapter(struct i2c_adapter *adap)
  1201. {
  1202. int res = -EINVAL;
  1203. /* Can't register until after driver model init */
  1204. if (WARN_ON(!is_registered)) {
  1205. res = -EAGAIN;
  1206. goto out_list;
  1207. }
  1208. /* Sanity checks */
  1209. if (WARN(!adap->name[0], "i2c adapter has no name"))
  1210. goto out_list;
  1211. if (!adap->algo) {
  1212. pr_err("adapter '%s': no algo supplied!\n", adap->name);
  1213. goto out_list;
  1214. }
  1215. if (!adap->lock_ops)
  1216. adap->lock_ops = &i2c_adapter_lock_ops;
  1217. adap->locked_flags = 0;
  1218. rt_mutex_init(&adap->bus_lock);
  1219. rt_mutex_init(&adap->mux_lock);
  1220. mutex_init(&adap->userspace_clients_lock);
  1221. INIT_LIST_HEAD(&adap->userspace_clients);
  1222. /* Set default timeout to 1 second if not already set */
  1223. if (adap->timeout == 0)
  1224. adap->timeout = HZ;
  1225. /* register soft irqs for Host Notify */
  1226. res = i2c_setup_host_notify_irq_domain(adap);
  1227. if (res) {
  1228. pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
  1229. adap->name, res);
  1230. goto out_list;
  1231. }
  1232. dev_set_name(&adap->dev, "i2c-%d", adap->nr);
  1233. adap->dev.bus = &i2c_bus_type;
  1234. adap->dev.type = &i2c_adapter_type;
  1235. res = device_register(&adap->dev);
  1236. if (res) {
  1237. pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
  1238. goto out_list;
  1239. }
  1240. res = i2c_setup_smbus_alert(adap);
  1241. if (res)
  1242. goto out_reg;
  1243. device_enable_async_suspend(&adap->dev);
  1244. pm_runtime_no_callbacks(&adap->dev);
  1245. pm_suspend_ignore_children(&adap->dev, true);
  1246. pm_runtime_enable(&adap->dev);
  1247. res = i2c_init_recovery(adap);
  1248. if (res == -EPROBE_DEFER)
  1249. goto out_reg;
  1250. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  1251. #ifdef CONFIG_I2C_COMPAT
  1252. res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
  1253. adap->dev.parent);
  1254. if (res)
  1255. dev_warn(&adap->dev,
  1256. "Failed to create compatibility class link\n");
  1257. #endif
  1258. /* create pre-declared device nodes */
  1259. of_i2c_register_devices(adap);
  1260. i2c_acpi_install_space_handler(adap);
  1261. i2c_acpi_register_devices(adap);
  1262. if (adap->nr < __i2c_first_dynamic_bus_num)
  1263. i2c_scan_static_board_info(adap);
  1264. /* Notify drivers */
  1265. mutex_lock(&core_lock);
  1266. bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
  1267. mutex_unlock(&core_lock);
  1268. return 0;
  1269. out_reg:
  1270. init_completion(&adap->dev_released);
  1271. device_unregister(&adap->dev);
  1272. wait_for_completion(&adap->dev_released);
  1273. out_list:
  1274. mutex_lock(&core_lock);
  1275. idr_remove(&i2c_adapter_idr, adap->nr);
  1276. mutex_unlock(&core_lock);
  1277. return res;
  1278. }
  1279. /**
  1280. * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
  1281. * @adap: the adapter to register (with adap->nr initialized)
  1282. * Context: can sleep
  1283. *
  1284. * See i2c_add_numbered_adapter() for details.
  1285. */
  1286. static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
  1287. {
  1288. int id;
  1289. mutex_lock(&core_lock);
  1290. id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
  1291. mutex_unlock(&core_lock);
  1292. if (WARN(id < 0, "couldn't get idr"))
  1293. return id == -ENOSPC ? -EBUSY : id;
  1294. return i2c_register_adapter(adap);
  1295. }
  1296. /**
  1297. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  1298. * @adapter: the adapter to add
  1299. * Context: can sleep
  1300. *
  1301. * This routine is used to declare an I2C adapter when its bus number
  1302. * doesn't matter or when its bus number is specified by an dt alias.
  1303. * Examples of bases when the bus number doesn't matter: I2C adapters
  1304. * dynamically added by USB links or PCI plugin cards.
  1305. *
  1306. * When this returns zero, a new bus number was allocated and stored
  1307. * in adap->nr, and the specified adapter became available for clients.
  1308. * Otherwise, a negative errno value is returned.
  1309. */
  1310. int i2c_add_adapter(struct i2c_adapter *adapter)
  1311. {
  1312. struct device *dev = &adapter->dev;
  1313. int id;
  1314. if (dev->of_node) {
  1315. id = of_alias_get_id(dev->of_node, "i2c");
  1316. if (id >= 0) {
  1317. adapter->nr = id;
  1318. return __i2c_add_numbered_adapter(adapter);
  1319. }
  1320. }
  1321. mutex_lock(&core_lock);
  1322. id = idr_alloc(&i2c_adapter_idr, adapter,
  1323. __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
  1324. mutex_unlock(&core_lock);
  1325. if (WARN(id < 0, "couldn't get idr"))
  1326. return id;
  1327. adapter->nr = id;
  1328. return i2c_register_adapter(adapter);
  1329. }
  1330. EXPORT_SYMBOL(i2c_add_adapter);
  1331. /**
  1332. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  1333. * @adap: the adapter to register (with adap->nr initialized)
  1334. * Context: can sleep
  1335. *
  1336. * This routine is used to declare an I2C adapter when its bus number
  1337. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  1338. * or otherwise built in to the system's mainboard, and where i2c_board_info
  1339. * is used to properly configure I2C devices.
  1340. *
  1341. * If the requested bus number is set to -1, then this function will behave
  1342. * identically to i2c_add_adapter, and will dynamically assign a bus number.
  1343. *
  1344. * If no devices have pre-been declared for this bus, then be sure to
  1345. * register the adapter before any dynamically allocated ones. Otherwise
  1346. * the required bus ID may not be available.
  1347. *
  1348. * When this returns zero, the specified adapter became available for
  1349. * clients using the bus number provided in adap->nr. Also, the table
  1350. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  1351. * and the appropriate driver model device nodes are created. Otherwise, a
  1352. * negative errno value is returned.
  1353. */
  1354. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  1355. {
  1356. if (adap->nr == -1) /* -1 means dynamically assign bus id */
  1357. return i2c_add_adapter(adap);
  1358. return __i2c_add_numbered_adapter(adap);
  1359. }
  1360. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  1361. static void i2c_do_del_adapter(struct i2c_driver *driver,
  1362. struct i2c_adapter *adapter)
  1363. {
  1364. struct i2c_client *client, *_n;
  1365. /* Remove the devices we created ourselves as the result of hardware
  1366. * probing (using a driver's detect method) */
  1367. list_for_each_entry_safe(client, _n, &driver->clients, detected) {
  1368. if (client->adapter == adapter) {
  1369. dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
  1370. client->name, client->addr);
  1371. list_del(&client->detected);
  1372. i2c_unregister_device(client);
  1373. }
  1374. }
  1375. }
  1376. static int __unregister_client(struct device *dev, void *dummy)
  1377. {
  1378. struct i2c_client *client = i2c_verify_client(dev);
  1379. if (client && strcmp(client->name, "dummy"))
  1380. i2c_unregister_device(client);
  1381. return 0;
  1382. }
  1383. static int __unregister_dummy(struct device *dev, void *dummy)
  1384. {
  1385. struct i2c_client *client = i2c_verify_client(dev);
  1386. i2c_unregister_device(client);
  1387. return 0;
  1388. }
  1389. static int __process_removed_adapter(struct device_driver *d, void *data)
  1390. {
  1391. i2c_do_del_adapter(to_i2c_driver(d), data);
  1392. return 0;
  1393. }
  1394. /**
  1395. * i2c_del_adapter - unregister I2C adapter
  1396. * @adap: the adapter being unregistered
  1397. * Context: can sleep
  1398. *
  1399. * This unregisters an I2C adapter which was previously registered
  1400. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  1401. */
  1402. void i2c_del_adapter(struct i2c_adapter *adap)
  1403. {
  1404. struct i2c_adapter *found;
  1405. struct i2c_client *client, *next;
  1406. /* First make sure that this adapter was ever added */
  1407. mutex_lock(&core_lock);
  1408. found = idr_find(&i2c_adapter_idr, adap->nr);
  1409. mutex_unlock(&core_lock);
  1410. if (found != adap) {
  1411. pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
  1412. return;
  1413. }
  1414. i2c_acpi_remove_space_handler(adap);
  1415. /* Tell drivers about this removal */
  1416. mutex_lock(&core_lock);
  1417. bus_for_each_drv(&i2c_bus_type, NULL, adap,
  1418. __process_removed_adapter);
  1419. mutex_unlock(&core_lock);
  1420. /* Remove devices instantiated from sysfs */
  1421. mutex_lock_nested(&adap->userspace_clients_lock,
  1422. i2c_adapter_depth(adap));
  1423. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  1424. detected) {
  1425. dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
  1426. client->addr);
  1427. list_del(&client->detected);
  1428. i2c_unregister_device(client);
  1429. }
  1430. mutex_unlock(&adap->userspace_clients_lock);
  1431. /* Detach any active clients. This can't fail, thus we do not
  1432. * check the returned value. This is a two-pass process, because
  1433. * we can't remove the dummy devices during the first pass: they
  1434. * could have been instantiated by real devices wishing to clean
  1435. * them up properly, so we give them a chance to do that first. */
  1436. device_for_each_child(&adap->dev, NULL, __unregister_client);
  1437. device_for_each_child(&adap->dev, NULL, __unregister_dummy);
  1438. #ifdef CONFIG_I2C_COMPAT
  1439. class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
  1440. adap->dev.parent);
  1441. #endif
  1442. /* device name is gone after device_unregister */
  1443. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  1444. pm_runtime_disable(&adap->dev);
  1445. i2c_host_notify_irq_teardown(adap);
  1446. /* wait until all references to the device are gone
  1447. *
  1448. * FIXME: This is old code and should ideally be replaced by an
  1449. * alternative which results in decoupling the lifetime of the struct
  1450. * device from the i2c_adapter, like spi or netdev do. Any solution
  1451. * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
  1452. */
  1453. init_completion(&adap->dev_released);
  1454. device_unregister(&adap->dev);
  1455. wait_for_completion(&adap->dev_released);
  1456. /* free bus id */
  1457. mutex_lock(&core_lock);
  1458. idr_remove(&i2c_adapter_idr, adap->nr);
  1459. mutex_unlock(&core_lock);
  1460. /* Clear the device structure in case this adapter is ever going to be
  1461. added again */
  1462. memset(&adap->dev, 0, sizeof(adap->dev));
  1463. }
  1464. EXPORT_SYMBOL(i2c_del_adapter);
  1465. static void devm_i2c_del_adapter(void *adapter)
  1466. {
  1467. i2c_del_adapter(adapter);
  1468. }
  1469. /**
  1470. * devm_i2c_add_adapter - device-managed variant of i2c_add_adapter()
  1471. * @dev: managing device for adding this I2C adapter
  1472. * @adapter: the adapter to add
  1473. * Context: can sleep
  1474. *
  1475. * Add adapter with dynamic bus number, same with i2c_add_adapter()
  1476. * but the adapter will be auto deleted on driver detach.
  1477. */
  1478. int devm_i2c_add_adapter(struct device *dev, struct i2c_adapter *adapter)
  1479. {
  1480. int ret;
  1481. ret = i2c_add_adapter(adapter);
  1482. if (ret)
  1483. return ret;
  1484. return devm_add_action_or_reset(dev, devm_i2c_del_adapter, adapter);
  1485. }
  1486. EXPORT_SYMBOL_GPL(devm_i2c_add_adapter);
  1487. static void i2c_parse_timing(struct device *dev, char *prop_name, u32 *cur_val_p,
  1488. u32 def_val, bool use_def)
  1489. {
  1490. int ret;
  1491. ret = device_property_read_u32(dev, prop_name, cur_val_p);
  1492. if (ret && use_def)
  1493. *cur_val_p = def_val;
  1494. dev_dbg(dev, "%s: %u\n", prop_name, *cur_val_p);
  1495. }
  1496. /**
  1497. * i2c_parse_fw_timings - get I2C related timing parameters from firmware
  1498. * @dev: The device to scan for I2C timing properties
  1499. * @t: the i2c_timings struct to be filled with values
  1500. * @use_defaults: bool to use sane defaults derived from the I2C specification
  1501. * when properties are not found, otherwise don't update
  1502. *
  1503. * Scan the device for the generic I2C properties describing timing parameters
  1504. * for the signal and fill the given struct with the results. If a property was
  1505. * not found and use_defaults was true, then maximum timings are assumed which
  1506. * are derived from the I2C specification. If use_defaults is not used, the
  1507. * results will be as before, so drivers can apply their own defaults before
  1508. * calling this helper. The latter is mainly intended for avoiding regressions
  1509. * of existing drivers which want to switch to this function. New drivers
  1510. * almost always should use the defaults.
  1511. */
  1512. void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
  1513. {
  1514. bool u = use_defaults;
  1515. u32 d;
  1516. i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz,
  1517. I2C_MAX_STANDARD_MODE_FREQ, u);
  1518. d = t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ ? 1000 :
  1519. t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
  1520. i2c_parse_timing(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns, d, u);
  1521. d = t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
  1522. i2c_parse_timing(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns, d, u);
  1523. i2c_parse_timing(dev, "i2c-scl-internal-delay-ns",
  1524. &t->scl_int_delay_ns, 0, u);
  1525. i2c_parse_timing(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns,
  1526. t->scl_fall_ns, u);
  1527. i2c_parse_timing(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns, 0, u);
  1528. i2c_parse_timing(dev, "i2c-digital-filter-width-ns",
  1529. &t->digital_filter_width_ns, 0, u);
  1530. i2c_parse_timing(dev, "i2c-analog-filter-cutoff-frequency",
  1531. &t->analog_filter_cutoff_freq_hz, 0, u);
  1532. }
  1533. EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
  1534. /* ------------------------------------------------------------------------- */
  1535. int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data))
  1536. {
  1537. int res;
  1538. mutex_lock(&core_lock);
  1539. res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
  1540. mutex_unlock(&core_lock);
  1541. return res;
  1542. }
  1543. EXPORT_SYMBOL_GPL(i2c_for_each_dev);
  1544. static int __process_new_driver(struct device *dev, void *data)
  1545. {
  1546. if (dev->type != &i2c_adapter_type)
  1547. return 0;
  1548. return i2c_do_add_adapter(data, to_i2c_adapter(dev));
  1549. }
  1550. /*
  1551. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  1552. * i2c slave chips, on a bus instance associated with some i2c_adapter.
  1553. */
  1554. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  1555. {
  1556. int res;
  1557. /* Can't register until after driver model init */
  1558. if (WARN_ON(!is_registered))
  1559. return -EAGAIN;
  1560. /* add the driver to the list of i2c drivers in the driver core */
  1561. driver->driver.owner = owner;
  1562. driver->driver.bus = &i2c_bus_type;
  1563. INIT_LIST_HEAD(&driver->clients);
  1564. /* When registration returns, the driver core
  1565. * will have called probe() for all matching-but-unbound devices.
  1566. */
  1567. res = driver_register(&driver->driver);
  1568. if (res)
  1569. return res;
  1570. pr_debug("driver [%s] registered\n", driver->driver.name);
  1571. /* Walk the adapters that are already present */
  1572. i2c_for_each_dev(driver, __process_new_driver);
  1573. return 0;
  1574. }
  1575. EXPORT_SYMBOL(i2c_register_driver);
  1576. static int __process_removed_driver(struct device *dev, void *data)
  1577. {
  1578. if (dev->type == &i2c_adapter_type)
  1579. i2c_do_del_adapter(data, to_i2c_adapter(dev));
  1580. return 0;
  1581. }
  1582. /**
  1583. * i2c_del_driver - unregister I2C driver
  1584. * @driver: the driver being unregistered
  1585. * Context: can sleep
  1586. */
  1587. void i2c_del_driver(struct i2c_driver *driver)
  1588. {
  1589. i2c_for_each_dev(driver, __process_removed_driver);
  1590. driver_unregister(&driver->driver);
  1591. pr_debug("driver [%s] unregistered\n", driver->driver.name);
  1592. }
  1593. EXPORT_SYMBOL(i2c_del_driver);
  1594. /* ------------------------------------------------------------------------- */
  1595. struct i2c_cmd_arg {
  1596. unsigned cmd;
  1597. void *arg;
  1598. };
  1599. static int i2c_cmd(struct device *dev, void *_arg)
  1600. {
  1601. struct i2c_client *client = i2c_verify_client(dev);
  1602. struct i2c_cmd_arg *arg = _arg;
  1603. struct i2c_driver *driver;
  1604. if (!client || !client->dev.driver)
  1605. return 0;
  1606. driver = to_i2c_driver(client->dev.driver);
  1607. if (driver->command)
  1608. driver->command(client, arg->cmd, arg->arg);
  1609. return 0;
  1610. }
  1611. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  1612. {
  1613. struct i2c_cmd_arg cmd_arg;
  1614. cmd_arg.cmd = cmd;
  1615. cmd_arg.arg = arg;
  1616. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  1617. }
  1618. EXPORT_SYMBOL(i2c_clients_command);
  1619. static int __init i2c_init(void)
  1620. {
  1621. int retval;
  1622. retval = of_alias_get_highest_id("i2c");
  1623. down_write(&__i2c_board_lock);
  1624. if (retval >= __i2c_first_dynamic_bus_num)
  1625. __i2c_first_dynamic_bus_num = retval + 1;
  1626. up_write(&__i2c_board_lock);
  1627. retval = bus_register(&i2c_bus_type);
  1628. if (retval)
  1629. return retval;
  1630. is_registered = true;
  1631. #ifdef CONFIG_I2C_COMPAT
  1632. i2c_adapter_compat_class = class_compat_register("i2c-adapter");
  1633. if (!i2c_adapter_compat_class) {
  1634. retval = -ENOMEM;
  1635. goto bus_err;
  1636. }
  1637. #endif
  1638. retval = i2c_add_driver(&dummy_driver);
  1639. if (retval)
  1640. goto class_err;
  1641. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  1642. WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
  1643. if (IS_ENABLED(CONFIG_ACPI))
  1644. WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
  1645. return 0;
  1646. class_err:
  1647. #ifdef CONFIG_I2C_COMPAT
  1648. class_compat_unregister(i2c_adapter_compat_class);
  1649. bus_err:
  1650. #endif
  1651. is_registered = false;
  1652. bus_unregister(&i2c_bus_type);
  1653. return retval;
  1654. }
  1655. static void __exit i2c_exit(void)
  1656. {
  1657. if (IS_ENABLED(CONFIG_ACPI))
  1658. WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
  1659. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  1660. WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
  1661. i2c_del_driver(&dummy_driver);
  1662. #ifdef CONFIG_I2C_COMPAT
  1663. class_compat_unregister(i2c_adapter_compat_class);
  1664. #endif
  1665. bus_unregister(&i2c_bus_type);
  1666. tracepoint_synchronize_unregister();
  1667. }
  1668. /* We must initialize early, because some subsystems register i2c drivers
  1669. * in subsys_initcall() code, but are linked (and initialized) before i2c.
  1670. */
  1671. postcore_initcall(i2c_init);
  1672. module_exit(i2c_exit);
  1673. /* ----------------------------------------------------
  1674. * the functional interface to the i2c busses.
  1675. * ----------------------------------------------------
  1676. */
  1677. /* Check if val is exceeding the quirk IFF quirk is non 0 */
  1678. #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
  1679. static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
  1680. {
  1681. dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
  1682. err_msg, msg->addr, msg->len,
  1683. msg->flags & I2C_M_RD ? "read" : "write");
  1684. return -EOPNOTSUPP;
  1685. }
  1686. static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1687. {
  1688. const struct i2c_adapter_quirks *q = adap->quirks;
  1689. int max_num = q->max_num_msgs, i;
  1690. bool do_len_check = true;
  1691. if (q->flags & I2C_AQ_COMB) {
  1692. max_num = 2;
  1693. /* special checks for combined messages */
  1694. if (num == 2) {
  1695. if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
  1696. return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
  1697. if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
  1698. return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
  1699. if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
  1700. return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
  1701. if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
  1702. return i2c_quirk_error(adap, &msgs[0], "msg too long");
  1703. if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
  1704. return i2c_quirk_error(adap, &msgs[1], "msg too long");
  1705. do_len_check = false;
  1706. }
  1707. }
  1708. if (i2c_quirk_exceeded(num, max_num))
  1709. return i2c_quirk_error(adap, &msgs[0], "too many messages");
  1710. for (i = 0; i < num; i++) {
  1711. u16 len = msgs[i].len;
  1712. if (msgs[i].flags & I2C_M_RD) {
  1713. if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
  1714. return i2c_quirk_error(adap, &msgs[i], "msg too long");
  1715. if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0)
  1716. return i2c_quirk_error(adap, &msgs[i], "no zero length");
  1717. } else {
  1718. if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
  1719. return i2c_quirk_error(adap, &msgs[i], "msg too long");
  1720. if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0)
  1721. return i2c_quirk_error(adap, &msgs[i], "no zero length");
  1722. }
  1723. }
  1724. return 0;
  1725. }
  1726. /**
  1727. * __i2c_transfer - unlocked flavor of i2c_transfer
  1728. * @adap: Handle to I2C bus
  1729. * @msgs: One or more messages to execute before STOP is issued to
  1730. * terminate the operation; each message begins with a START.
  1731. * @num: Number of messages to be executed.
  1732. *
  1733. * Returns negative errno, else the number of messages executed.
  1734. *
  1735. * Adapter lock must be held when calling this function. No debug logging
  1736. * takes place. adap->algo->master_xfer existence isn't checked.
  1737. */
  1738. int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1739. {
  1740. unsigned long orig_jiffies;
  1741. int ret, try;
  1742. if (WARN_ON(!msgs || num < 1))
  1743. return -EINVAL;
  1744. ret = __i2c_check_suspended(adap);
  1745. if (ret)
  1746. return ret;
  1747. if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
  1748. return -EOPNOTSUPP;
  1749. /*
  1750. * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets
  1751. * enabled. This is an efficient way of keeping the for-loop from
  1752. * being executed when not needed.
  1753. */
  1754. if (static_branch_unlikely(&i2c_trace_msg_key)) {
  1755. int i;
  1756. for (i = 0; i < num; i++)
  1757. if (msgs[i].flags & I2C_M_RD)
  1758. trace_i2c_read(adap, &msgs[i], i);
  1759. else
  1760. trace_i2c_write(adap, &msgs[i], i);
  1761. }
  1762. /* Retry automatically on arbitration loss */
  1763. orig_jiffies = jiffies;
  1764. for (ret = 0, try = 0; try <= adap->retries; try++) {
  1765. if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic)
  1766. ret = adap->algo->master_xfer_atomic(adap, msgs, num);
  1767. else
  1768. ret = adap->algo->master_xfer(adap, msgs, num);
  1769. if (ret != -EAGAIN)
  1770. break;
  1771. if (time_after(jiffies, orig_jiffies + adap->timeout))
  1772. break;
  1773. }
  1774. if (static_branch_unlikely(&i2c_trace_msg_key)) {
  1775. int i;
  1776. for (i = 0; i < ret; i++)
  1777. if (msgs[i].flags & I2C_M_RD)
  1778. trace_i2c_reply(adap, &msgs[i], i);
  1779. trace_i2c_result(adap, num, ret);
  1780. }
  1781. return ret;
  1782. }
  1783. EXPORT_SYMBOL(__i2c_transfer);
  1784. /**
  1785. * i2c_transfer - execute a single or combined I2C message
  1786. * @adap: Handle to I2C bus
  1787. * @msgs: One or more messages to execute before STOP is issued to
  1788. * terminate the operation; each message begins with a START.
  1789. * @num: Number of messages to be executed.
  1790. *
  1791. * Returns negative errno, else the number of messages executed.
  1792. *
  1793. * Note that there is no requirement that each message be sent to
  1794. * the same slave address, although that is the most common model.
  1795. */
  1796. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1797. {
  1798. int ret;
  1799. if (!adap->algo->master_xfer) {
  1800. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  1801. return -EOPNOTSUPP;
  1802. }
  1803. /* REVISIT the fault reporting model here is weak:
  1804. *
  1805. * - When we get an error after receiving N bytes from a slave,
  1806. * there is no way to report "N".
  1807. *
  1808. * - When we get a NAK after transmitting N bytes to a slave,
  1809. * there is no way to report "N" ... or to let the master
  1810. * continue executing the rest of this combined message, if
  1811. * that's the appropriate response.
  1812. *
  1813. * - When for example "num" is two and we successfully complete
  1814. * the first message but get an error part way through the
  1815. * second, it's unclear whether that should be reported as
  1816. * one (discarding status on the second message) or errno
  1817. * (discarding status on the first one).
  1818. */
  1819. ret = __i2c_lock_bus_helper(adap);
  1820. if (ret)
  1821. return ret;
  1822. ret = __i2c_transfer(adap, msgs, num);
  1823. i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
  1824. return ret;
  1825. }
  1826. EXPORT_SYMBOL(i2c_transfer);
  1827. /**
  1828. * i2c_transfer_buffer_flags - issue a single I2C message transferring data
  1829. * to/from a buffer
  1830. * @client: Handle to slave device
  1831. * @buf: Where the data is stored
  1832. * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
  1833. * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
  1834. *
  1835. * Returns negative errno, or else the number of bytes transferred.
  1836. */
  1837. int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
  1838. int count, u16 flags)
  1839. {
  1840. int ret;
  1841. struct i2c_msg msg = {
  1842. .addr = client->addr,
  1843. .flags = flags | (client->flags & I2C_M_TEN),
  1844. .len = count,
  1845. .buf = buf,
  1846. };
  1847. ret = i2c_transfer(client->adapter, &msg, 1);
  1848. /*
  1849. * If everything went ok (i.e. 1 msg transferred), return #bytes
  1850. * transferred, else error code.
  1851. */
  1852. return (ret == 1) ? count : ret;
  1853. }
  1854. EXPORT_SYMBOL(i2c_transfer_buffer_flags);
  1855. /**
  1856. * i2c_get_device_id - get manufacturer, part id and die revision of a device
  1857. * @client: The device to query
  1858. * @id: The queried information
  1859. *
  1860. * Returns negative errno on error, zero on success.
  1861. */
  1862. int i2c_get_device_id(const struct i2c_client *client,
  1863. struct i2c_device_identity *id)
  1864. {
  1865. struct i2c_adapter *adap = client->adapter;
  1866. union i2c_smbus_data raw_id;
  1867. int ret;
  1868. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  1869. return -EOPNOTSUPP;
  1870. raw_id.block[0] = 3;
  1871. ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0,
  1872. I2C_SMBUS_READ, client->addr << 1,
  1873. I2C_SMBUS_I2C_BLOCK_DATA, &raw_id);
  1874. if (ret)
  1875. return ret;
  1876. id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4);
  1877. id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3);
  1878. id->die_revision = raw_id.block[3] & 0x7;
  1879. return 0;
  1880. }
  1881. EXPORT_SYMBOL_GPL(i2c_get_device_id);
  1882. /* ----------------------------------------------------
  1883. * the i2c address scanning function
  1884. * Will not work for 10-bit addresses!
  1885. * ----------------------------------------------------
  1886. */
  1887. /*
  1888. * Legacy default probe function, mostly relevant for SMBus. The default
  1889. * probe method is a quick write, but it is known to corrupt the 24RF08
  1890. * EEPROMs due to a state machine bug, and could also irreversibly
  1891. * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
  1892. * we use a short byte read instead. Also, some bus drivers don't implement
  1893. * quick write, so we fallback to a byte read in that case too.
  1894. * On x86, there is another special case for FSC hardware monitoring chips,
  1895. * which want regular byte reads (address 0x73.) Fortunately, these are the
  1896. * only known chips using this I2C address on PC hardware.
  1897. * Returns 1 if probe succeeded, 0 if not.
  1898. */
  1899. static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
  1900. {
  1901. int err;
  1902. union i2c_smbus_data dummy;
  1903. #ifdef CONFIG_X86
  1904. if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
  1905. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  1906. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1907. I2C_SMBUS_BYTE_DATA, &dummy);
  1908. else
  1909. #endif
  1910. if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
  1911. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
  1912. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
  1913. I2C_SMBUS_QUICK, NULL);
  1914. else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
  1915. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1916. I2C_SMBUS_BYTE, &dummy);
  1917. else {
  1918. dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
  1919. addr);
  1920. err = -EOPNOTSUPP;
  1921. }
  1922. return err >= 0;
  1923. }
  1924. static int i2c_detect_address(struct i2c_client *temp_client,
  1925. struct i2c_driver *driver)
  1926. {
  1927. struct i2c_board_info info;
  1928. struct i2c_adapter *adapter = temp_client->adapter;
  1929. int addr = temp_client->addr;
  1930. int err;
  1931. /* Make sure the address is valid */
  1932. err = i2c_check_7bit_addr_validity_strict(addr);
  1933. if (err) {
  1934. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  1935. addr);
  1936. return err;
  1937. }
  1938. /* Skip if already in use (7 bit, no need to encode flags) */
  1939. if (i2c_check_addr_busy(adapter, addr))
  1940. return 0;
  1941. /* Make sure there is something at this address */
  1942. if (!i2c_default_probe(adapter, addr))
  1943. return 0;
  1944. /* Finally call the custom detection function */
  1945. memset(&info, 0, sizeof(struct i2c_board_info));
  1946. info.addr = addr;
  1947. err = driver->detect(temp_client, &info);
  1948. if (err) {
  1949. /* -ENODEV is returned if the detection fails. We catch it
  1950. here as this isn't an error. */
  1951. return err == -ENODEV ? 0 : err;
  1952. }
  1953. /* Consistency check */
  1954. if (info.type[0] == '\0') {
  1955. dev_err(&adapter->dev,
  1956. "%s detection function provided no name for 0x%x\n",
  1957. driver->driver.name, addr);
  1958. } else {
  1959. struct i2c_client *client;
  1960. /* Detection succeeded, instantiate the device */
  1961. if (adapter->class & I2C_CLASS_DEPRECATED)
  1962. dev_warn(&adapter->dev,
  1963. "This adapter will soon drop class based instantiation of devices. "
  1964. "Please make sure client 0x%02x gets instantiated by other means. "
  1965. "Check 'Documentation/i2c/instantiating-devices.rst' for details.\n",
  1966. info.addr);
  1967. dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
  1968. info.type, info.addr);
  1969. client = i2c_new_client_device(adapter, &info);
  1970. if (!IS_ERR(client))
  1971. list_add_tail(&client->detected, &driver->clients);
  1972. else
  1973. dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
  1974. info.type, info.addr);
  1975. }
  1976. return 0;
  1977. }
  1978. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
  1979. {
  1980. const unsigned short *address_list;
  1981. struct i2c_client *temp_client;
  1982. int i, err = 0;
  1983. address_list = driver->address_list;
  1984. if (!driver->detect || !address_list)
  1985. return 0;
  1986. /* Warn that the adapter lost class based instantiation */
  1987. if (adapter->class == I2C_CLASS_DEPRECATED) {
  1988. dev_dbg(&adapter->dev,
  1989. "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
  1990. "If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n",
  1991. driver->driver.name);
  1992. return 0;
  1993. }
  1994. /* Stop here if the classes do not match */
  1995. if (!(adapter->class & driver->class))
  1996. return 0;
  1997. /* Set up a temporary client to help detect callback */
  1998. temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  1999. if (!temp_client)
  2000. return -ENOMEM;
  2001. temp_client->adapter = adapter;
  2002. for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
  2003. dev_dbg(&adapter->dev,
  2004. "found normal entry for adapter %d, addr 0x%02x\n",
  2005. i2c_adapter_id(adapter), address_list[i]);
  2006. temp_client->addr = address_list[i];
  2007. err = i2c_detect_address(temp_client, driver);
  2008. if (unlikely(err))
  2009. break;
  2010. }
  2011. kfree(temp_client);
  2012. return err;
  2013. }
  2014. int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
  2015. {
  2016. return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  2017. I2C_SMBUS_QUICK, NULL) >= 0;
  2018. }
  2019. EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
  2020. struct i2c_client *
  2021. i2c_new_scanned_device(struct i2c_adapter *adap,
  2022. struct i2c_board_info *info,
  2023. unsigned short const *addr_list,
  2024. int (*probe)(struct i2c_adapter *adap, unsigned short addr))
  2025. {
  2026. int i;
  2027. if (!probe)
  2028. probe = i2c_default_probe;
  2029. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  2030. /* Check address validity */
  2031. if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
  2032. dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
  2033. addr_list[i]);
  2034. continue;
  2035. }
  2036. /* Check address availability (7 bit, no need to encode flags) */
  2037. if (i2c_check_addr_busy(adap, addr_list[i])) {
  2038. dev_dbg(&adap->dev,
  2039. "Address 0x%02x already in use, not probing\n",
  2040. addr_list[i]);
  2041. continue;
  2042. }
  2043. /* Test address responsiveness */
  2044. if (probe(adap, addr_list[i]))
  2045. break;
  2046. }
  2047. if (addr_list[i] == I2C_CLIENT_END) {
  2048. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  2049. return ERR_PTR(-ENODEV);
  2050. }
  2051. info->addr = addr_list[i];
  2052. return i2c_new_client_device(adap, info);
  2053. }
  2054. EXPORT_SYMBOL_GPL(i2c_new_scanned_device);
  2055. struct i2c_adapter *i2c_get_adapter(int nr)
  2056. {
  2057. struct i2c_adapter *adapter;
  2058. mutex_lock(&core_lock);
  2059. adapter = idr_find(&i2c_adapter_idr, nr);
  2060. if (!adapter)
  2061. goto exit;
  2062. if (try_module_get(adapter->owner))
  2063. get_device(&adapter->dev);
  2064. else
  2065. adapter = NULL;
  2066. exit:
  2067. mutex_unlock(&core_lock);
  2068. return adapter;
  2069. }
  2070. EXPORT_SYMBOL(i2c_get_adapter);
  2071. void i2c_put_adapter(struct i2c_adapter *adap)
  2072. {
  2073. if (!adap)
  2074. return;
  2075. module_put(adap->owner);
  2076. /* Should be last, otherwise we risk use-after-free with 'adap' */
  2077. put_device(&adap->dev);
  2078. }
  2079. EXPORT_SYMBOL(i2c_put_adapter);
  2080. /**
  2081. * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
  2082. * @msg: the message to be checked
  2083. * @threshold: the minimum number of bytes for which using DMA makes sense.
  2084. * Should at least be 1.
  2085. *
  2086. * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
  2087. * Or a valid pointer to be used with DMA. After use, release it by
  2088. * calling i2c_put_dma_safe_msg_buf().
  2089. *
  2090. * This function must only be called from process context!
  2091. */
  2092. u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
  2093. {
  2094. /* also skip 0-length msgs for bogus thresholds of 0 */
  2095. if (!threshold)
  2096. pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n",
  2097. msg->addr);
  2098. if (msg->len < threshold || msg->len == 0)
  2099. return NULL;
  2100. if (msg->flags & I2C_M_DMA_SAFE)
  2101. return msg->buf;
  2102. pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
  2103. msg->addr, msg->len);
  2104. if (msg->flags & I2C_M_RD)
  2105. return kzalloc(msg->len, GFP_KERNEL);
  2106. else
  2107. return kmemdup(msg->buf, msg->len, GFP_KERNEL);
  2108. }
  2109. EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
  2110. /**
  2111. * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
  2112. * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
  2113. * @msg: the message which the buffer corresponds to
  2114. * @xferred: bool saying if the message was transferred
  2115. */
  2116. void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred)
  2117. {
  2118. if (!buf || buf == msg->buf)
  2119. return;
  2120. if (xferred && msg->flags & I2C_M_RD)
  2121. memcpy(msg->buf, buf, msg->len);
  2122. kfree(buf);
  2123. }
  2124. EXPORT_SYMBOL_GPL(i2c_put_dma_safe_msg_buf);
  2125. MODULE_AUTHOR("Simon G. Vogl <[email protected]>");
  2126. MODULE_DESCRIPTION("I2C-Bus main module");
  2127. MODULE_LICENSE("GPL");