regmap.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  1. /*
  2. * Register map access API
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/export.h>
  15. #include <linux/mutex.h>
  16. #include <linux/err.h>
  17. #include <linux/of.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/sched.h>
  20. #include <linux/delay.h>
  21. #include <linux/log2.h>
  22. #include <linux/hwspinlock.h>
  23. #define CREATE_TRACE_POINTS
  24. #include "trace.h"
  25. #include "internal.h"
  26. /*
  27. * Sometimes for failures during very early init the trace
  28. * infrastructure isn't available early enough to be used. For this
  29. * sort of problem defining LOG_DEVICE will add printks for basic
  30. * register I/O on a specific device.
  31. */
  32. #undef LOG_DEVICE
  33. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  34. unsigned int mask, unsigned int val,
  35. bool *change, bool force_write);
  36. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  37. unsigned int *val);
  38. static int _regmap_bus_read(void *context, unsigned int reg,
  39. unsigned int *val);
  40. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  41. unsigned int val);
  42. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  43. unsigned int val);
  44. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  45. unsigned int val);
  46. bool regmap_reg_in_ranges(unsigned int reg,
  47. const struct regmap_range *ranges,
  48. unsigned int nranges)
  49. {
  50. const struct regmap_range *r;
  51. int i;
  52. for (i = 0, r = ranges; i < nranges; i++, r++)
  53. if (regmap_reg_in_range(reg, r))
  54. return true;
  55. return false;
  56. }
  57. EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
  58. bool regmap_check_range_table(struct regmap *map, unsigned int reg,
  59. const struct regmap_access_table *table)
  60. {
  61. /* Check "no ranges" first */
  62. if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
  63. return false;
  64. /* In case zero "yes ranges" are supplied, any reg is OK */
  65. if (!table->n_yes_ranges)
  66. return true;
  67. return regmap_reg_in_ranges(reg, table->yes_ranges,
  68. table->n_yes_ranges);
  69. }
  70. EXPORT_SYMBOL_GPL(regmap_check_range_table);
  71. bool regmap_writeable(struct regmap *map, unsigned int reg)
  72. {
  73. if (map->max_register && reg > map->max_register)
  74. return false;
  75. if (map->writeable_reg)
  76. return map->writeable_reg(map->dev, reg);
  77. if (map->wr_table)
  78. return regmap_check_range_table(map, reg, map->wr_table);
  79. return true;
  80. }
  81. bool regmap_cached(struct regmap *map, unsigned int reg)
  82. {
  83. int ret;
  84. unsigned int val;
  85. if (map->cache_type == REGCACHE_NONE)
  86. return false;
  87. if (!map->cache_ops)
  88. return false;
  89. if (map->max_register && reg > map->max_register)
  90. return false;
  91. map->lock(map->lock_arg);
  92. ret = regcache_read(map, reg, &val);
  93. map->unlock(map->lock_arg);
  94. if (ret)
  95. return false;
  96. return true;
  97. }
  98. bool regmap_readable(struct regmap *map, unsigned int reg)
  99. {
  100. if (!map->reg_read)
  101. return false;
  102. if (map->max_register && reg > map->max_register)
  103. return false;
  104. if (map->format.format_write)
  105. return false;
  106. if (map->readable_reg)
  107. return map->readable_reg(map->dev, reg);
  108. if (map->rd_table)
  109. return regmap_check_range_table(map, reg, map->rd_table);
  110. return true;
  111. }
  112. bool regmap_volatile(struct regmap *map, unsigned int reg)
  113. {
  114. if (!map->format.format_write && !regmap_readable(map, reg))
  115. return false;
  116. if (map->volatile_reg)
  117. return map->volatile_reg(map->dev, reg);
  118. if (map->volatile_table)
  119. return regmap_check_range_table(map, reg, map->volatile_table);
  120. if (map->cache_ops)
  121. return false;
  122. else
  123. return true;
  124. }
  125. bool regmap_precious(struct regmap *map, unsigned int reg)
  126. {
  127. if (!regmap_readable(map, reg))
  128. return false;
  129. if (map->precious_reg)
  130. return map->precious_reg(map->dev, reg);
  131. if (map->precious_table)
  132. return regmap_check_range_table(map, reg, map->precious_table);
  133. return false;
  134. }
  135. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  136. size_t num)
  137. {
  138. unsigned int i;
  139. for (i = 0; i < num; i++)
  140. if (!regmap_volatile(map, reg + regmap_get_offset(map, i)))
  141. return false;
  142. return true;
  143. }
  144. static void regmap_format_2_6_write(struct regmap *map,
  145. unsigned int reg, unsigned int val)
  146. {
  147. u8 *out = map->work_buf;
  148. *out = (reg << 6) | val;
  149. }
  150. static void regmap_format_4_12_write(struct regmap *map,
  151. unsigned int reg, unsigned int val)
  152. {
  153. __be16 *out = map->work_buf;
  154. *out = cpu_to_be16((reg << 12) | val);
  155. }
  156. static void regmap_format_7_9_write(struct regmap *map,
  157. unsigned int reg, unsigned int val)
  158. {
  159. __be16 *out = map->work_buf;
  160. *out = cpu_to_be16((reg << 9) | val);
  161. }
  162. static void regmap_format_10_14_write(struct regmap *map,
  163. unsigned int reg, unsigned int val)
  164. {
  165. u8 *out = map->work_buf;
  166. out[2] = val;
  167. out[1] = (val >> 8) | (reg << 6);
  168. out[0] = reg >> 2;
  169. }
  170. static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
  171. {
  172. u8 *b = buf;
  173. b[0] = val << shift;
  174. }
  175. static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
  176. {
  177. __be16 *b = buf;
  178. b[0] = cpu_to_be16(val << shift);
  179. }
  180. static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
  181. {
  182. __le16 *b = buf;
  183. b[0] = cpu_to_le16(val << shift);
  184. }
  185. static void regmap_format_16_native(void *buf, unsigned int val,
  186. unsigned int shift)
  187. {
  188. *(u16 *)buf = val << shift;
  189. }
  190. static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
  191. {
  192. u8 *b = buf;
  193. val <<= shift;
  194. b[0] = val >> 16;
  195. b[1] = val >> 8;
  196. b[2] = val;
  197. }
  198. static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
  199. {
  200. __be32 *b = buf;
  201. b[0] = cpu_to_be32(val << shift);
  202. }
  203. static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
  204. {
  205. __le32 *b = buf;
  206. b[0] = cpu_to_le32(val << shift);
  207. }
  208. static void regmap_format_32_native(void *buf, unsigned int val,
  209. unsigned int shift)
  210. {
  211. *(u32 *)buf = val << shift;
  212. }
  213. #ifdef CONFIG_64BIT
  214. static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
  215. {
  216. __be64 *b = buf;
  217. b[0] = cpu_to_be64((u64)val << shift);
  218. }
  219. static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
  220. {
  221. __le64 *b = buf;
  222. b[0] = cpu_to_le64((u64)val << shift);
  223. }
  224. static void regmap_format_64_native(void *buf, unsigned int val,
  225. unsigned int shift)
  226. {
  227. *(u64 *)buf = (u64)val << shift;
  228. }
  229. #endif
  230. static void regmap_parse_inplace_noop(void *buf)
  231. {
  232. }
  233. static unsigned int regmap_parse_8(const void *buf)
  234. {
  235. const u8 *b = buf;
  236. return b[0];
  237. }
  238. static unsigned int regmap_parse_16_be(const void *buf)
  239. {
  240. const __be16 *b = buf;
  241. return be16_to_cpu(b[0]);
  242. }
  243. static unsigned int regmap_parse_16_le(const void *buf)
  244. {
  245. const __le16 *b = buf;
  246. return le16_to_cpu(b[0]);
  247. }
  248. static void regmap_parse_16_be_inplace(void *buf)
  249. {
  250. __be16 *b = buf;
  251. b[0] = be16_to_cpu(b[0]);
  252. }
  253. static void regmap_parse_16_le_inplace(void *buf)
  254. {
  255. __le16 *b = buf;
  256. b[0] = le16_to_cpu(b[0]);
  257. }
  258. static unsigned int regmap_parse_16_native(const void *buf)
  259. {
  260. return *(u16 *)buf;
  261. }
  262. static unsigned int regmap_parse_24(const void *buf)
  263. {
  264. const u8 *b = buf;
  265. unsigned int ret = b[2];
  266. ret |= ((unsigned int)b[1]) << 8;
  267. ret |= ((unsigned int)b[0]) << 16;
  268. return ret;
  269. }
  270. static unsigned int regmap_parse_32_be(const void *buf)
  271. {
  272. const __be32 *b = buf;
  273. return be32_to_cpu(b[0]);
  274. }
  275. static unsigned int regmap_parse_32_le(const void *buf)
  276. {
  277. const __le32 *b = buf;
  278. return le32_to_cpu(b[0]);
  279. }
  280. static void regmap_parse_32_be_inplace(void *buf)
  281. {
  282. __be32 *b = buf;
  283. b[0] = be32_to_cpu(b[0]);
  284. }
  285. static void regmap_parse_32_le_inplace(void *buf)
  286. {
  287. __le32 *b = buf;
  288. b[0] = le32_to_cpu(b[0]);
  289. }
  290. static unsigned int regmap_parse_32_native(const void *buf)
  291. {
  292. return *(u32 *)buf;
  293. }
  294. #ifdef CONFIG_64BIT
  295. static unsigned int regmap_parse_64_be(const void *buf)
  296. {
  297. const __be64 *b = buf;
  298. return be64_to_cpu(b[0]);
  299. }
  300. static unsigned int regmap_parse_64_le(const void *buf)
  301. {
  302. const __le64 *b = buf;
  303. return le64_to_cpu(b[0]);
  304. }
  305. static void regmap_parse_64_be_inplace(void *buf)
  306. {
  307. __be64 *b = buf;
  308. b[0] = be64_to_cpu(b[0]);
  309. }
  310. static void regmap_parse_64_le_inplace(void *buf)
  311. {
  312. __le64 *b = buf;
  313. b[0] = le64_to_cpu(b[0]);
  314. }
  315. static unsigned int regmap_parse_64_native(const void *buf)
  316. {
  317. return *(u64 *)buf;
  318. }
  319. #endif
  320. static void regmap_lock_hwlock(void *__map)
  321. {
  322. struct regmap *map = __map;
  323. hwspin_lock_timeout(map->hwlock, UINT_MAX);
  324. }
  325. static void regmap_lock_hwlock_irq(void *__map)
  326. {
  327. struct regmap *map = __map;
  328. hwspin_lock_timeout_irq(map->hwlock, UINT_MAX);
  329. }
  330. static void regmap_lock_hwlock_irqsave(void *__map)
  331. {
  332. struct regmap *map = __map;
  333. hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
  334. &map->spinlock_flags);
  335. }
  336. static void regmap_unlock_hwlock(void *__map)
  337. {
  338. struct regmap *map = __map;
  339. hwspin_unlock(map->hwlock);
  340. }
  341. static void regmap_unlock_hwlock_irq(void *__map)
  342. {
  343. struct regmap *map = __map;
  344. hwspin_unlock_irq(map->hwlock);
  345. }
  346. static void regmap_unlock_hwlock_irqrestore(void *__map)
  347. {
  348. struct regmap *map = __map;
  349. hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags);
  350. }
  351. static void regmap_lock_unlock_none(void *__map)
  352. {
  353. }
  354. static void regmap_lock_mutex(void *__map)
  355. {
  356. struct regmap *map = __map;
  357. mutex_lock(&map->mutex);
  358. }
  359. static void regmap_unlock_mutex(void *__map)
  360. {
  361. struct regmap *map = __map;
  362. mutex_unlock(&map->mutex);
  363. }
  364. static void regmap_lock_spinlock(void *__map)
  365. __acquires(&map->spinlock)
  366. {
  367. struct regmap *map = __map;
  368. unsigned long flags;
  369. spin_lock_irqsave(&map->spinlock, flags);
  370. map->spinlock_flags = flags;
  371. }
  372. static void regmap_unlock_spinlock(void *__map)
  373. __releases(&map->spinlock)
  374. {
  375. struct regmap *map = __map;
  376. spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
  377. }
  378. static void dev_get_regmap_release(struct device *dev, void *res)
  379. {
  380. /*
  381. * We don't actually have anything to do here; the goal here
  382. * is not to manage the regmap but to provide a simple way to
  383. * get the regmap back given a struct device.
  384. */
  385. }
  386. static bool _regmap_range_add(struct regmap *map,
  387. struct regmap_range_node *data)
  388. {
  389. struct rb_root *root = &map->range_tree;
  390. struct rb_node **new = &(root->rb_node), *parent = NULL;
  391. while (*new) {
  392. struct regmap_range_node *this =
  393. rb_entry(*new, struct regmap_range_node, node);
  394. parent = *new;
  395. if (data->range_max < this->range_min)
  396. new = &((*new)->rb_left);
  397. else if (data->range_min > this->range_max)
  398. new = &((*new)->rb_right);
  399. else
  400. return false;
  401. }
  402. rb_link_node(&data->node, parent, new);
  403. rb_insert_color(&data->node, root);
  404. return true;
  405. }
  406. static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
  407. unsigned int reg)
  408. {
  409. struct rb_node *node = map->range_tree.rb_node;
  410. while (node) {
  411. struct regmap_range_node *this =
  412. rb_entry(node, struct regmap_range_node, node);
  413. if (reg < this->range_min)
  414. node = node->rb_left;
  415. else if (reg > this->range_max)
  416. node = node->rb_right;
  417. else
  418. return this;
  419. }
  420. return NULL;
  421. }
  422. static void regmap_range_exit(struct regmap *map)
  423. {
  424. struct rb_node *next;
  425. struct regmap_range_node *range_node;
  426. next = rb_first(&map->range_tree);
  427. while (next) {
  428. range_node = rb_entry(next, struct regmap_range_node, node);
  429. next = rb_next(&range_node->node);
  430. rb_erase(&range_node->node, &map->range_tree);
  431. kfree(range_node);
  432. }
  433. kfree(map->selector_work_buf);
  434. }
  435. int regmap_attach_dev(struct device *dev, struct regmap *map,
  436. const struct regmap_config *config)
  437. {
  438. struct regmap **m;
  439. map->dev = dev;
  440. regmap_debugfs_init(map, config->name);
  441. /* Add a devres resource for dev_get_regmap() */
  442. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  443. if (!m) {
  444. regmap_debugfs_exit(map);
  445. return -ENOMEM;
  446. }
  447. *m = map;
  448. devres_add(dev, m);
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(regmap_attach_dev);
  452. static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
  453. const struct regmap_config *config)
  454. {
  455. enum regmap_endian endian;
  456. /* Retrieve the endianness specification from the regmap config */
  457. endian = config->reg_format_endian;
  458. /* If the regmap config specified a non-default value, use that */
  459. if (endian != REGMAP_ENDIAN_DEFAULT)
  460. return endian;
  461. /* Retrieve the endianness specification from the bus config */
  462. if (bus && bus->reg_format_endian_default)
  463. endian = bus->reg_format_endian_default;
  464. /* If the bus specified a non-default value, use that */
  465. if (endian != REGMAP_ENDIAN_DEFAULT)
  466. return endian;
  467. /* Use this if no other value was found */
  468. return REGMAP_ENDIAN_BIG;
  469. }
  470. enum regmap_endian regmap_get_val_endian(struct device *dev,
  471. const struct regmap_bus *bus,
  472. const struct regmap_config *config)
  473. {
  474. struct device_node *np;
  475. enum regmap_endian endian;
  476. /* Retrieve the endianness specification from the regmap config */
  477. endian = config->val_format_endian;
  478. /* If the regmap config specified a non-default value, use that */
  479. if (endian != REGMAP_ENDIAN_DEFAULT)
  480. return endian;
  481. /* If the dev and dev->of_node exist try to get endianness from DT */
  482. if (dev && dev->of_node) {
  483. np = dev->of_node;
  484. /* Parse the device's DT node for an endianness specification */
  485. if (of_property_read_bool(np, "big-endian"))
  486. endian = REGMAP_ENDIAN_BIG;
  487. else if (of_property_read_bool(np, "little-endian"))
  488. endian = REGMAP_ENDIAN_LITTLE;
  489. else if (of_property_read_bool(np, "native-endian"))
  490. endian = REGMAP_ENDIAN_NATIVE;
  491. /* If the endianness was specified in DT, use that */
  492. if (endian != REGMAP_ENDIAN_DEFAULT)
  493. return endian;
  494. }
  495. /* Retrieve the endianness specification from the bus config */
  496. if (bus && bus->val_format_endian_default)
  497. endian = bus->val_format_endian_default;
  498. /* If the bus specified a non-default value, use that */
  499. if (endian != REGMAP_ENDIAN_DEFAULT)
  500. return endian;
  501. /* Use this if no other value was found */
  502. return REGMAP_ENDIAN_BIG;
  503. }
  504. EXPORT_SYMBOL_GPL(regmap_get_val_endian);
  505. struct regmap *__regmap_init(struct device *dev,
  506. const struct regmap_bus *bus,
  507. void *bus_context,
  508. const struct regmap_config *config,
  509. struct lock_class_key *lock_key,
  510. const char *lock_name)
  511. {
  512. struct regmap *map;
  513. int ret = -EINVAL;
  514. enum regmap_endian reg_endian, val_endian;
  515. int i, j;
  516. if (!config)
  517. goto err;
  518. map = kzalloc(sizeof(*map), GFP_KERNEL);
  519. if (map == NULL) {
  520. ret = -ENOMEM;
  521. goto err;
  522. }
  523. if (config->name) {
  524. map->name = kstrdup_const(config->name, GFP_KERNEL);
  525. if (!map->name) {
  526. ret = -ENOMEM;
  527. goto err_map;
  528. }
  529. }
  530. if (config->disable_locking) {
  531. map->lock = map->unlock = regmap_lock_unlock_none;
  532. regmap_debugfs_disable(map);
  533. } else if (config->lock && config->unlock) {
  534. map->lock = config->lock;
  535. map->unlock = config->unlock;
  536. map->lock_arg = config->lock_arg;
  537. } else if (config->use_hwlock) {
  538. map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
  539. if (!map->hwlock) {
  540. ret = -ENXIO;
  541. goto err_name;
  542. }
  543. switch (config->hwlock_mode) {
  544. case HWLOCK_IRQSTATE:
  545. map->lock = regmap_lock_hwlock_irqsave;
  546. map->unlock = regmap_unlock_hwlock_irqrestore;
  547. break;
  548. case HWLOCK_IRQ:
  549. map->lock = regmap_lock_hwlock_irq;
  550. map->unlock = regmap_unlock_hwlock_irq;
  551. break;
  552. default:
  553. map->lock = regmap_lock_hwlock;
  554. map->unlock = regmap_unlock_hwlock;
  555. break;
  556. }
  557. map->lock_arg = map;
  558. } else {
  559. if ((bus && bus->fast_io) ||
  560. config->fast_io) {
  561. spin_lock_init(&map->spinlock);
  562. map->lock = regmap_lock_spinlock;
  563. map->unlock = regmap_unlock_spinlock;
  564. lockdep_set_class_and_name(&map->spinlock,
  565. lock_key, lock_name);
  566. } else {
  567. mutex_init(&map->mutex);
  568. map->lock = regmap_lock_mutex;
  569. map->unlock = regmap_unlock_mutex;
  570. lockdep_set_class_and_name(&map->mutex,
  571. lock_key, lock_name);
  572. }
  573. map->lock_arg = map;
  574. }
  575. /*
  576. * When we write in fast-paths with regmap_bulk_write() don't allocate
  577. * scratch buffers with sleeping allocations.
  578. */
  579. if ((bus && bus->fast_io) || config->fast_io)
  580. map->alloc_flags = GFP_ATOMIC;
  581. else
  582. map->alloc_flags = GFP_KERNEL;
  583. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  584. map->format.pad_bytes = config->pad_bits / 8;
  585. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  586. map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
  587. config->val_bits + config->pad_bits, 8);
  588. map->reg_shift = config->pad_bits % 8;
  589. if (config->reg_stride)
  590. map->reg_stride = config->reg_stride;
  591. else
  592. map->reg_stride = 1;
  593. if (is_power_of_2(map->reg_stride))
  594. map->reg_stride_order = ilog2(map->reg_stride);
  595. else
  596. map->reg_stride_order = -1;
  597. map->use_single_read = config->use_single_rw || !bus || !bus->read;
  598. map->use_single_write = config->use_single_rw || !bus || !bus->write;
  599. map->can_multi_write = config->can_multi_write && bus && bus->write;
  600. if (bus) {
  601. map->max_raw_read = bus->max_raw_read;
  602. map->max_raw_write = bus->max_raw_write;
  603. }
  604. map->dev = dev;
  605. map->bus = bus;
  606. map->bus_context = bus_context;
  607. map->max_register = config->max_register;
  608. map->wr_table = config->wr_table;
  609. map->rd_table = config->rd_table;
  610. map->volatile_table = config->volatile_table;
  611. map->precious_table = config->precious_table;
  612. map->writeable_reg = config->writeable_reg;
  613. map->readable_reg = config->readable_reg;
  614. map->volatile_reg = config->volatile_reg;
  615. map->precious_reg = config->precious_reg;
  616. map->cache_type = config->cache_type;
  617. spin_lock_init(&map->async_lock);
  618. INIT_LIST_HEAD(&map->async_list);
  619. INIT_LIST_HEAD(&map->async_free);
  620. init_waitqueue_head(&map->async_waitq);
  621. if (config->read_flag_mask ||
  622. config->write_flag_mask ||
  623. config->zero_flag_mask) {
  624. map->read_flag_mask = config->read_flag_mask;
  625. map->write_flag_mask = config->write_flag_mask;
  626. } else if (bus) {
  627. map->read_flag_mask = bus->read_flag_mask;
  628. }
  629. if (!bus) {
  630. map->reg_read = config->reg_read;
  631. map->reg_write = config->reg_write;
  632. map->defer_caching = false;
  633. goto skip_format_initialization;
  634. } else if (!bus->read || !bus->write) {
  635. map->reg_read = _regmap_bus_reg_read;
  636. map->reg_write = _regmap_bus_reg_write;
  637. map->defer_caching = false;
  638. goto skip_format_initialization;
  639. } else {
  640. map->reg_read = _regmap_bus_read;
  641. map->reg_update_bits = bus->reg_update_bits;
  642. }
  643. reg_endian = regmap_get_reg_endian(bus, config);
  644. val_endian = regmap_get_val_endian(dev, bus, config);
  645. switch (config->reg_bits + map->reg_shift) {
  646. case 2:
  647. switch (config->val_bits) {
  648. case 6:
  649. map->format.format_write = regmap_format_2_6_write;
  650. break;
  651. default:
  652. goto err_hwlock;
  653. }
  654. break;
  655. case 4:
  656. switch (config->val_bits) {
  657. case 12:
  658. map->format.format_write = regmap_format_4_12_write;
  659. break;
  660. default:
  661. goto err_hwlock;
  662. }
  663. break;
  664. case 7:
  665. switch (config->val_bits) {
  666. case 9:
  667. map->format.format_write = regmap_format_7_9_write;
  668. break;
  669. default:
  670. goto err_hwlock;
  671. }
  672. break;
  673. case 10:
  674. switch (config->val_bits) {
  675. case 14:
  676. map->format.format_write = regmap_format_10_14_write;
  677. break;
  678. default:
  679. goto err_hwlock;
  680. }
  681. break;
  682. case 8:
  683. map->format.format_reg = regmap_format_8;
  684. break;
  685. case 16:
  686. switch (reg_endian) {
  687. case REGMAP_ENDIAN_BIG:
  688. map->format.format_reg = regmap_format_16_be;
  689. break;
  690. case REGMAP_ENDIAN_LITTLE:
  691. map->format.format_reg = regmap_format_16_le;
  692. break;
  693. case REGMAP_ENDIAN_NATIVE:
  694. map->format.format_reg = regmap_format_16_native;
  695. break;
  696. default:
  697. goto err_hwlock;
  698. }
  699. break;
  700. case 24:
  701. if (reg_endian != REGMAP_ENDIAN_BIG)
  702. goto err_hwlock;
  703. map->format.format_reg = regmap_format_24;
  704. break;
  705. case 32:
  706. switch (reg_endian) {
  707. case REGMAP_ENDIAN_BIG:
  708. map->format.format_reg = regmap_format_32_be;
  709. break;
  710. case REGMAP_ENDIAN_LITTLE:
  711. map->format.format_reg = regmap_format_32_le;
  712. break;
  713. case REGMAP_ENDIAN_NATIVE:
  714. map->format.format_reg = regmap_format_32_native;
  715. break;
  716. default:
  717. goto err_hwlock;
  718. }
  719. break;
  720. #ifdef CONFIG_64BIT
  721. case 64:
  722. switch (reg_endian) {
  723. case REGMAP_ENDIAN_BIG:
  724. map->format.format_reg = regmap_format_64_be;
  725. break;
  726. case REGMAP_ENDIAN_LITTLE:
  727. map->format.format_reg = regmap_format_64_le;
  728. break;
  729. case REGMAP_ENDIAN_NATIVE:
  730. map->format.format_reg = regmap_format_64_native;
  731. break;
  732. default:
  733. goto err_hwlock;
  734. }
  735. break;
  736. #endif
  737. default:
  738. goto err_hwlock;
  739. }
  740. if (val_endian == REGMAP_ENDIAN_NATIVE)
  741. map->format.parse_inplace = regmap_parse_inplace_noop;
  742. switch (config->val_bits) {
  743. case 8:
  744. map->format.format_val = regmap_format_8;
  745. map->format.parse_val = regmap_parse_8;
  746. map->format.parse_inplace = regmap_parse_inplace_noop;
  747. break;
  748. case 16:
  749. switch (val_endian) {
  750. case REGMAP_ENDIAN_BIG:
  751. map->format.format_val = regmap_format_16_be;
  752. map->format.parse_val = regmap_parse_16_be;
  753. map->format.parse_inplace = regmap_parse_16_be_inplace;
  754. break;
  755. case REGMAP_ENDIAN_LITTLE:
  756. map->format.format_val = regmap_format_16_le;
  757. map->format.parse_val = regmap_parse_16_le;
  758. map->format.parse_inplace = regmap_parse_16_le_inplace;
  759. break;
  760. case REGMAP_ENDIAN_NATIVE:
  761. map->format.format_val = regmap_format_16_native;
  762. map->format.parse_val = regmap_parse_16_native;
  763. break;
  764. default:
  765. goto err_hwlock;
  766. }
  767. break;
  768. case 24:
  769. if (val_endian != REGMAP_ENDIAN_BIG)
  770. goto err_hwlock;
  771. map->format.format_val = regmap_format_24;
  772. map->format.parse_val = regmap_parse_24;
  773. break;
  774. case 32:
  775. switch (val_endian) {
  776. case REGMAP_ENDIAN_BIG:
  777. map->format.format_val = regmap_format_32_be;
  778. map->format.parse_val = regmap_parse_32_be;
  779. map->format.parse_inplace = regmap_parse_32_be_inplace;
  780. break;
  781. case REGMAP_ENDIAN_LITTLE:
  782. map->format.format_val = regmap_format_32_le;
  783. map->format.parse_val = regmap_parse_32_le;
  784. map->format.parse_inplace = regmap_parse_32_le_inplace;
  785. break;
  786. case REGMAP_ENDIAN_NATIVE:
  787. map->format.format_val = regmap_format_32_native;
  788. map->format.parse_val = regmap_parse_32_native;
  789. break;
  790. default:
  791. goto err_hwlock;
  792. }
  793. break;
  794. #ifdef CONFIG_64BIT
  795. case 64:
  796. switch (val_endian) {
  797. case REGMAP_ENDIAN_BIG:
  798. map->format.format_val = regmap_format_64_be;
  799. map->format.parse_val = regmap_parse_64_be;
  800. map->format.parse_inplace = regmap_parse_64_be_inplace;
  801. break;
  802. case REGMAP_ENDIAN_LITTLE:
  803. map->format.format_val = regmap_format_64_le;
  804. map->format.parse_val = regmap_parse_64_le;
  805. map->format.parse_inplace = regmap_parse_64_le_inplace;
  806. break;
  807. case REGMAP_ENDIAN_NATIVE:
  808. map->format.format_val = regmap_format_64_native;
  809. map->format.parse_val = regmap_parse_64_native;
  810. break;
  811. default:
  812. goto err_hwlock;
  813. }
  814. break;
  815. #endif
  816. }
  817. if (map->format.format_write) {
  818. if ((reg_endian != REGMAP_ENDIAN_BIG) ||
  819. (val_endian != REGMAP_ENDIAN_BIG))
  820. goto err_hwlock;
  821. map->use_single_write = true;
  822. }
  823. if (!map->format.format_write &&
  824. !(map->format.format_reg && map->format.format_val))
  825. goto err_hwlock;
  826. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  827. if (map->work_buf == NULL) {
  828. ret = -ENOMEM;
  829. goto err_hwlock;
  830. }
  831. if (map->format.format_write) {
  832. map->defer_caching = false;
  833. map->reg_write = _regmap_bus_formatted_write;
  834. } else if (map->format.format_val) {
  835. map->defer_caching = true;
  836. map->reg_write = _regmap_bus_raw_write;
  837. }
  838. skip_format_initialization:
  839. map->range_tree = RB_ROOT;
  840. for (i = 0; i < config->num_ranges; i++) {
  841. const struct regmap_range_cfg *range_cfg = &config->ranges[i];
  842. struct regmap_range_node *new;
  843. /* Sanity check */
  844. if (range_cfg->range_max < range_cfg->range_min) {
  845. dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
  846. range_cfg->range_max, range_cfg->range_min);
  847. goto err_range;
  848. }
  849. if (range_cfg->range_max > map->max_register) {
  850. dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
  851. range_cfg->range_max, map->max_register);
  852. goto err_range;
  853. }
  854. if (range_cfg->selector_reg > map->max_register) {
  855. dev_err(map->dev,
  856. "Invalid range %d: selector out of map\n", i);
  857. goto err_range;
  858. }
  859. if (range_cfg->window_len == 0) {
  860. dev_err(map->dev, "Invalid range %d: window_len 0\n",
  861. i);
  862. goto err_range;
  863. }
  864. /* Make sure, that this register range has no selector
  865. or data window within its boundary */
  866. for (j = 0; j < config->num_ranges; j++) {
  867. unsigned sel_reg = config->ranges[j].selector_reg;
  868. unsigned win_min = config->ranges[j].window_start;
  869. unsigned win_max = win_min +
  870. config->ranges[j].window_len - 1;
  871. /* Allow data window inside its own virtual range */
  872. if (j == i)
  873. continue;
  874. if (range_cfg->range_min <= sel_reg &&
  875. sel_reg <= range_cfg->range_max) {
  876. dev_err(map->dev,
  877. "Range %d: selector for %d in window\n",
  878. i, j);
  879. goto err_range;
  880. }
  881. if (!(win_max < range_cfg->range_min ||
  882. win_min > range_cfg->range_max)) {
  883. dev_err(map->dev,
  884. "Range %d: window for %d in window\n",
  885. i, j);
  886. goto err_range;
  887. }
  888. }
  889. new = kzalloc(sizeof(*new), GFP_KERNEL);
  890. if (new == NULL) {
  891. ret = -ENOMEM;
  892. goto err_range;
  893. }
  894. new->map = map;
  895. new->name = range_cfg->name;
  896. new->range_min = range_cfg->range_min;
  897. new->range_max = range_cfg->range_max;
  898. new->selector_reg = range_cfg->selector_reg;
  899. new->selector_mask = range_cfg->selector_mask;
  900. new->selector_shift = range_cfg->selector_shift;
  901. new->window_start = range_cfg->window_start;
  902. new->window_len = range_cfg->window_len;
  903. if (!_regmap_range_add(map, new)) {
  904. dev_err(map->dev, "Failed to add range %d\n", i);
  905. kfree(new);
  906. goto err_range;
  907. }
  908. if (map->selector_work_buf == NULL) {
  909. map->selector_work_buf =
  910. kzalloc(map->format.buf_size, GFP_KERNEL);
  911. if (map->selector_work_buf == NULL) {
  912. ret = -ENOMEM;
  913. goto err_range;
  914. }
  915. }
  916. }
  917. ret = regcache_init(map, config);
  918. if (ret != 0)
  919. goto err_range;
  920. if (dev) {
  921. ret = regmap_attach_dev(dev, map, config);
  922. if (ret != 0)
  923. goto err_regcache;
  924. } else {
  925. regmap_debugfs_init(map, config->name);
  926. }
  927. return map;
  928. err_regcache:
  929. regcache_exit(map);
  930. err_range:
  931. regmap_range_exit(map);
  932. kfree(map->work_buf);
  933. err_hwlock:
  934. if (map->hwlock)
  935. hwspin_lock_free(map->hwlock);
  936. err_name:
  937. kfree_const(map->name);
  938. err_map:
  939. kfree(map);
  940. err:
  941. return ERR_PTR(ret);
  942. }
  943. EXPORT_SYMBOL_GPL(__regmap_init);
  944. static void devm_regmap_release(struct device *dev, void *res)
  945. {
  946. regmap_exit(*(struct regmap **)res);
  947. }
  948. struct regmap *__devm_regmap_init(struct device *dev,
  949. const struct regmap_bus *bus,
  950. void *bus_context,
  951. const struct regmap_config *config,
  952. struct lock_class_key *lock_key,
  953. const char *lock_name)
  954. {
  955. struct regmap **ptr, *regmap;
  956. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  957. if (!ptr)
  958. return ERR_PTR(-ENOMEM);
  959. regmap = __regmap_init(dev, bus, bus_context, config,
  960. lock_key, lock_name);
  961. if (!IS_ERR(regmap)) {
  962. *ptr = regmap;
  963. devres_add(dev, ptr);
  964. } else {
  965. devres_free(ptr);
  966. }
  967. return regmap;
  968. }
  969. EXPORT_SYMBOL_GPL(__devm_regmap_init);
  970. static void regmap_field_init(struct regmap_field *rm_field,
  971. struct regmap *regmap, struct reg_field reg_field)
  972. {
  973. rm_field->regmap = regmap;
  974. rm_field->reg = reg_field.reg;
  975. rm_field->shift = reg_field.lsb;
  976. rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
  977. rm_field->id_size = reg_field.id_size;
  978. rm_field->id_offset = reg_field.id_offset;
  979. }
  980. /**
  981. * devm_regmap_field_alloc() - Allocate and initialise a register field.
  982. *
  983. * @dev: Device that will be interacted with
  984. * @regmap: regmap bank in which this register field is located.
  985. * @reg_field: Register field with in the bank.
  986. *
  987. * The return value will be an ERR_PTR() on error or a valid pointer
  988. * to a struct regmap_field. The regmap_field will be automatically freed
  989. * by the device management code.
  990. */
  991. struct regmap_field *devm_regmap_field_alloc(struct device *dev,
  992. struct regmap *regmap, struct reg_field reg_field)
  993. {
  994. struct regmap_field *rm_field = devm_kzalloc(dev,
  995. sizeof(*rm_field), GFP_KERNEL);
  996. if (!rm_field)
  997. return ERR_PTR(-ENOMEM);
  998. regmap_field_init(rm_field, regmap, reg_field);
  999. return rm_field;
  1000. }
  1001. EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
  1002. /**
  1003. * devm_regmap_field_free() - Free a register field allocated using
  1004. * devm_regmap_field_alloc.
  1005. *
  1006. * @dev: Device that will be interacted with
  1007. * @field: regmap field which should be freed.
  1008. *
  1009. * Free register field allocated using devm_regmap_field_alloc(). Usually
  1010. * drivers need not call this function, as the memory allocated via devm
  1011. * will be freed as per device-driver life-cyle.
  1012. */
  1013. void devm_regmap_field_free(struct device *dev,
  1014. struct regmap_field *field)
  1015. {
  1016. devm_kfree(dev, field);
  1017. }
  1018. EXPORT_SYMBOL_GPL(devm_regmap_field_free);
  1019. /**
  1020. * regmap_field_alloc() - Allocate and initialise a register field.
  1021. *
  1022. * @regmap: regmap bank in which this register field is located.
  1023. * @reg_field: Register field with in the bank.
  1024. *
  1025. * The return value will be an ERR_PTR() on error or a valid pointer
  1026. * to a struct regmap_field. The regmap_field should be freed by the
  1027. * user once its finished working with it using regmap_field_free().
  1028. */
  1029. struct regmap_field *regmap_field_alloc(struct regmap *regmap,
  1030. struct reg_field reg_field)
  1031. {
  1032. struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
  1033. if (!rm_field)
  1034. return ERR_PTR(-ENOMEM);
  1035. regmap_field_init(rm_field, regmap, reg_field);
  1036. return rm_field;
  1037. }
  1038. EXPORT_SYMBOL_GPL(regmap_field_alloc);
  1039. /**
  1040. * regmap_field_free() - Free register field allocated using
  1041. * regmap_field_alloc.
  1042. *
  1043. * @field: regmap field which should be freed.
  1044. */
  1045. void regmap_field_free(struct regmap_field *field)
  1046. {
  1047. kfree(field);
  1048. }
  1049. EXPORT_SYMBOL_GPL(regmap_field_free);
  1050. /**
  1051. * regmap_reinit_cache() - Reinitialise the current register cache
  1052. *
  1053. * @map: Register map to operate on.
  1054. * @config: New configuration. Only the cache data will be used.
  1055. *
  1056. * Discard any existing register cache for the map and initialize a
  1057. * new cache. This can be used to restore the cache to defaults or to
  1058. * update the cache configuration to reflect runtime discovery of the
  1059. * hardware.
  1060. *
  1061. * No explicit locking is done here, the user needs to ensure that
  1062. * this function will not race with other calls to regmap.
  1063. */
  1064. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  1065. {
  1066. regcache_exit(map);
  1067. regmap_debugfs_exit(map);
  1068. map->max_register = config->max_register;
  1069. map->writeable_reg = config->writeable_reg;
  1070. map->readable_reg = config->readable_reg;
  1071. map->volatile_reg = config->volatile_reg;
  1072. map->precious_reg = config->precious_reg;
  1073. map->cache_type = config->cache_type;
  1074. regmap_debugfs_init(map, config->name);
  1075. map->cache_bypass = false;
  1076. map->cache_only = false;
  1077. return regcache_init(map, config);
  1078. }
  1079. EXPORT_SYMBOL_GPL(regmap_reinit_cache);
  1080. /**
  1081. * regmap_exit() - Free a previously allocated register map
  1082. *
  1083. * @map: Register map to operate on.
  1084. */
  1085. void regmap_exit(struct regmap *map)
  1086. {
  1087. struct regmap_async *async;
  1088. regcache_exit(map);
  1089. regmap_debugfs_exit(map);
  1090. regmap_range_exit(map);
  1091. if (map->bus && map->bus->free_context)
  1092. map->bus->free_context(map->bus_context);
  1093. kfree(map->work_buf);
  1094. while (!list_empty(&map->async_free)) {
  1095. async = list_first_entry_or_null(&map->async_free,
  1096. struct regmap_async,
  1097. list);
  1098. list_del(&async->list);
  1099. kfree(async->work_buf);
  1100. kfree(async);
  1101. }
  1102. if (map->hwlock)
  1103. hwspin_lock_free(map->hwlock);
  1104. kfree_const(map->name);
  1105. kfree(map);
  1106. }
  1107. EXPORT_SYMBOL_GPL(regmap_exit);
  1108. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  1109. {
  1110. struct regmap **r = res;
  1111. if (!r || !*r) {
  1112. WARN_ON(!r || !*r);
  1113. return 0;
  1114. }
  1115. /* If the user didn't specify a name match any */
  1116. if (data)
  1117. return (*r)->name == data;
  1118. else
  1119. return 1;
  1120. }
  1121. /**
  1122. * dev_get_regmap() - Obtain the regmap (if any) for a device
  1123. *
  1124. * @dev: Device to retrieve the map for
  1125. * @name: Optional name for the register map, usually NULL.
  1126. *
  1127. * Returns the regmap for the device if one is present, or NULL. If
  1128. * name is specified then it must match the name specified when
  1129. * registering the device, if it is NULL then the first regmap found
  1130. * will be used. Devices with multiple register maps are very rare,
  1131. * generic code should normally not need to specify a name.
  1132. */
  1133. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  1134. {
  1135. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  1136. dev_get_regmap_match, (void *)name);
  1137. if (!r)
  1138. return NULL;
  1139. return *r;
  1140. }
  1141. EXPORT_SYMBOL_GPL(dev_get_regmap);
  1142. /**
  1143. * regmap_get_device() - Obtain the device from a regmap
  1144. *
  1145. * @map: Register map to operate on.
  1146. *
  1147. * Returns the underlying device that the regmap has been created for.
  1148. */
  1149. struct device *regmap_get_device(struct regmap *map)
  1150. {
  1151. return map->dev;
  1152. }
  1153. EXPORT_SYMBOL_GPL(regmap_get_device);
  1154. static int _regmap_select_page(struct regmap *map, unsigned int *reg,
  1155. struct regmap_range_node *range,
  1156. unsigned int val_num)
  1157. {
  1158. void *orig_work_buf;
  1159. unsigned int win_offset;
  1160. unsigned int win_page;
  1161. bool page_chg;
  1162. int ret;
  1163. win_offset = (*reg - range->range_min) % range->window_len;
  1164. win_page = (*reg - range->range_min) / range->window_len;
  1165. if (val_num > 1) {
  1166. /* Bulk write shouldn't cross range boundary */
  1167. if (*reg + val_num - 1 > range->range_max)
  1168. return -EINVAL;
  1169. /* ... or single page boundary */
  1170. if (val_num > range->window_len - win_offset)
  1171. return -EINVAL;
  1172. }
  1173. /* It is possible to have selector register inside data window.
  1174. In that case, selector register is located on every page and
  1175. it needs no page switching, when accessed alone. */
  1176. if (val_num > 1 ||
  1177. range->window_start + win_offset != range->selector_reg) {
  1178. /* Use separate work_buf during page switching */
  1179. orig_work_buf = map->work_buf;
  1180. map->work_buf = map->selector_work_buf;
  1181. ret = _regmap_update_bits(map, range->selector_reg,
  1182. range->selector_mask,
  1183. win_page << range->selector_shift,
  1184. &page_chg, false);
  1185. map->work_buf = orig_work_buf;
  1186. if (ret != 0)
  1187. return ret;
  1188. }
  1189. *reg = range->window_start + win_offset;
  1190. return 0;
  1191. }
  1192. static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
  1193. unsigned long mask)
  1194. {
  1195. u8 *buf;
  1196. int i;
  1197. if (!mask || !map->work_buf)
  1198. return;
  1199. buf = map->work_buf;
  1200. for (i = 0; i < max_bytes; i++)
  1201. buf[i] |= (mask >> (8 * i)) & 0xff;
  1202. }
  1203. static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
  1204. const void *val, size_t val_len)
  1205. {
  1206. struct regmap_range_node *range;
  1207. unsigned long flags;
  1208. void *work_val = map->work_buf + map->format.reg_bytes +
  1209. map->format.pad_bytes;
  1210. void *buf;
  1211. int ret = -ENOTSUPP;
  1212. size_t len;
  1213. int i;
  1214. WARN_ON(!map->bus);
  1215. /* Check for unwritable registers before we start */
  1216. if (map->writeable_reg)
  1217. for (i = 0; i < val_len / map->format.val_bytes; i++)
  1218. if (!map->writeable_reg(map->dev,
  1219. reg + regmap_get_offset(map, i)))
  1220. return -EINVAL;
  1221. if (!map->cache_bypass && map->format.parse_val) {
  1222. unsigned int ival;
  1223. int val_bytes = map->format.val_bytes;
  1224. for (i = 0; i < val_len / val_bytes; i++) {
  1225. ival = map->format.parse_val(val + (i * val_bytes));
  1226. ret = regcache_write(map,
  1227. reg + regmap_get_offset(map, i),
  1228. ival);
  1229. if (ret) {
  1230. dev_err(map->dev,
  1231. "Error in caching of register: %x ret: %d\n",
  1232. reg + i, ret);
  1233. return ret;
  1234. }
  1235. }
  1236. if (map->cache_only) {
  1237. map->cache_dirty = true;
  1238. return 0;
  1239. }
  1240. }
  1241. range = _regmap_range_lookup(map, reg);
  1242. if (range) {
  1243. int val_num = val_len / map->format.val_bytes;
  1244. int win_offset = (reg - range->range_min) % range->window_len;
  1245. int win_residue = range->window_len - win_offset;
  1246. /* If the write goes beyond the end of the window split it */
  1247. while (val_num > win_residue) {
  1248. dev_dbg(map->dev, "Writing window %d/%zu\n",
  1249. win_residue, val_len / map->format.val_bytes);
  1250. ret = _regmap_raw_write_impl(map, reg, val,
  1251. win_residue *
  1252. map->format.val_bytes);
  1253. if (ret != 0)
  1254. return ret;
  1255. reg += win_residue;
  1256. val_num -= win_residue;
  1257. val += win_residue * map->format.val_bytes;
  1258. val_len -= win_residue * map->format.val_bytes;
  1259. win_offset = (reg - range->range_min) %
  1260. range->window_len;
  1261. win_residue = range->window_len - win_offset;
  1262. }
  1263. ret = _regmap_select_page(map, &reg, range, val_num);
  1264. if (ret != 0)
  1265. return ret;
  1266. }
  1267. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1268. regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
  1269. map->write_flag_mask);
  1270. /*
  1271. * Essentially all I/O mechanisms will be faster with a single
  1272. * buffer to write. Since register syncs often generate raw
  1273. * writes of single registers optimise that case.
  1274. */
  1275. if (val != work_val && val_len == map->format.val_bytes) {
  1276. memcpy(work_val, val, map->format.val_bytes);
  1277. val = work_val;
  1278. }
  1279. if (map->async && map->bus->async_write) {
  1280. struct regmap_async *async;
  1281. trace_regmap_async_write_start(map, reg, val_len);
  1282. spin_lock_irqsave(&map->async_lock, flags);
  1283. async = list_first_entry_or_null(&map->async_free,
  1284. struct regmap_async,
  1285. list);
  1286. if (async)
  1287. list_del(&async->list);
  1288. spin_unlock_irqrestore(&map->async_lock, flags);
  1289. if (!async) {
  1290. async = map->bus->async_alloc();
  1291. if (!async)
  1292. return -ENOMEM;
  1293. async->work_buf = kzalloc(map->format.buf_size,
  1294. GFP_KERNEL | GFP_DMA);
  1295. if (!async->work_buf) {
  1296. kfree(async);
  1297. return -ENOMEM;
  1298. }
  1299. }
  1300. async->map = map;
  1301. /* If the caller supplied the value we can use it safely. */
  1302. memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
  1303. map->format.reg_bytes + map->format.val_bytes);
  1304. spin_lock_irqsave(&map->async_lock, flags);
  1305. list_add_tail(&async->list, &map->async_list);
  1306. spin_unlock_irqrestore(&map->async_lock, flags);
  1307. if (val != work_val)
  1308. ret = map->bus->async_write(map->bus_context,
  1309. async->work_buf,
  1310. map->format.reg_bytes +
  1311. map->format.pad_bytes,
  1312. val, val_len, async);
  1313. else
  1314. ret = map->bus->async_write(map->bus_context,
  1315. async->work_buf,
  1316. map->format.reg_bytes +
  1317. map->format.pad_bytes +
  1318. val_len, NULL, 0, async);
  1319. if (ret != 0) {
  1320. dev_err(map->dev, "Failed to schedule write: %d\n",
  1321. ret);
  1322. spin_lock_irqsave(&map->async_lock, flags);
  1323. list_move(&async->list, &map->async_free);
  1324. spin_unlock_irqrestore(&map->async_lock, flags);
  1325. }
  1326. return ret;
  1327. }
  1328. trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
  1329. /* If we're doing a single register write we can probably just
  1330. * send the work_buf directly, otherwise try to do a gather
  1331. * write.
  1332. */
  1333. if (val == work_val)
  1334. ret = map->bus->write(map->bus_context, map->work_buf,
  1335. map->format.reg_bytes +
  1336. map->format.pad_bytes +
  1337. val_len);
  1338. else if (map->bus->gather_write)
  1339. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  1340. map->format.reg_bytes +
  1341. map->format.pad_bytes,
  1342. val, val_len);
  1343. /* If that didn't work fall back on linearising by hand. */
  1344. if (ret == -ENOTSUPP) {
  1345. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  1346. buf = kzalloc(len, GFP_KERNEL);
  1347. if (!buf)
  1348. return -ENOMEM;
  1349. memcpy(buf, map->work_buf, map->format.reg_bytes);
  1350. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  1351. val, val_len);
  1352. ret = map->bus->write(map->bus_context, buf, len);
  1353. kfree(buf);
  1354. } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
  1355. /* regcache_drop_region() takes lock that we already have,
  1356. * thus call map->cache_ops->drop() directly
  1357. */
  1358. if (map->cache_ops && map->cache_ops->drop)
  1359. map->cache_ops->drop(map, reg, reg + 1);
  1360. }
  1361. trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
  1362. return ret;
  1363. }
  1364. /**
  1365. * regmap_can_raw_write - Test if regmap_raw_write() is supported
  1366. *
  1367. * @map: Map to check.
  1368. */
  1369. bool regmap_can_raw_write(struct regmap *map)
  1370. {
  1371. return map->bus && map->bus->write && map->format.format_val &&
  1372. map->format.format_reg;
  1373. }
  1374. EXPORT_SYMBOL_GPL(regmap_can_raw_write);
  1375. /**
  1376. * regmap_get_raw_read_max - Get the maximum size we can read
  1377. *
  1378. * @map: Map to check.
  1379. */
  1380. size_t regmap_get_raw_read_max(struct regmap *map)
  1381. {
  1382. return map->max_raw_read;
  1383. }
  1384. EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
  1385. /**
  1386. * regmap_get_raw_write_max - Get the maximum size we can read
  1387. *
  1388. * @map: Map to check.
  1389. */
  1390. size_t regmap_get_raw_write_max(struct regmap *map)
  1391. {
  1392. return map->max_raw_write;
  1393. }
  1394. EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
  1395. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  1396. unsigned int val)
  1397. {
  1398. int ret;
  1399. struct regmap_range_node *range;
  1400. struct regmap *map = context;
  1401. WARN_ON(!map->bus || !map->format.format_write);
  1402. range = _regmap_range_lookup(map, reg);
  1403. if (range) {
  1404. ret = _regmap_select_page(map, &reg, range, 1);
  1405. if (ret != 0)
  1406. return ret;
  1407. }
  1408. map->format.format_write(map, reg, val);
  1409. trace_regmap_hw_write_start(map, reg, 1);
  1410. ret = map->bus->write(map->bus_context, map->work_buf,
  1411. map->format.buf_size);
  1412. trace_regmap_hw_write_done(map, reg, 1);
  1413. return ret;
  1414. }
  1415. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  1416. unsigned int val)
  1417. {
  1418. struct regmap *map = context;
  1419. return map->bus->reg_write(map->bus_context, reg, val);
  1420. }
  1421. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  1422. unsigned int val)
  1423. {
  1424. struct regmap *map = context;
  1425. WARN_ON(!map->bus || !map->format.format_val);
  1426. map->format.format_val(map->work_buf + map->format.reg_bytes
  1427. + map->format.pad_bytes, val, 0);
  1428. return _regmap_raw_write_impl(map, reg,
  1429. map->work_buf +
  1430. map->format.reg_bytes +
  1431. map->format.pad_bytes,
  1432. map->format.val_bytes);
  1433. }
  1434. static inline void *_regmap_map_get_context(struct regmap *map)
  1435. {
  1436. return (map->bus) ? map : map->bus_context;
  1437. }
  1438. int _regmap_write(struct regmap *map, unsigned int reg,
  1439. unsigned int val)
  1440. {
  1441. int ret;
  1442. void *context = _regmap_map_get_context(map);
  1443. if (!regmap_writeable(map, reg))
  1444. return -EIO;
  1445. if (!map->cache_bypass && !map->defer_caching) {
  1446. ret = regcache_write(map, reg, val);
  1447. if (ret != 0)
  1448. return ret;
  1449. if (map->cache_only) {
  1450. map->cache_dirty = true;
  1451. return 0;
  1452. }
  1453. }
  1454. #ifdef LOG_DEVICE
  1455. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1456. dev_info(map->dev, "%x <= %x\n", reg, val);
  1457. #endif
  1458. trace_regmap_reg_write(map, reg, val);
  1459. return map->reg_write(context, reg, val);
  1460. }
  1461. /**
  1462. * regmap_write() - Write a value to a single register
  1463. *
  1464. * @map: Register map to write to
  1465. * @reg: Register to write to
  1466. * @val: Value to be written
  1467. *
  1468. * A value of zero will be returned on success, a negative errno will
  1469. * be returned in error cases.
  1470. */
  1471. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  1472. {
  1473. int ret;
  1474. if (!IS_ALIGNED(reg, map->reg_stride))
  1475. return -EINVAL;
  1476. map->lock(map->lock_arg);
  1477. ret = _regmap_write(map, reg, val);
  1478. map->unlock(map->lock_arg);
  1479. return ret;
  1480. }
  1481. EXPORT_SYMBOL_GPL(regmap_write);
  1482. /**
  1483. * regmap_write_async() - Write a value to a single register asynchronously
  1484. *
  1485. * @map: Register map to write to
  1486. * @reg: Register to write to
  1487. * @val: Value to be written
  1488. *
  1489. * A value of zero will be returned on success, a negative errno will
  1490. * be returned in error cases.
  1491. */
  1492. int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
  1493. {
  1494. int ret;
  1495. if (!IS_ALIGNED(reg, map->reg_stride))
  1496. return -EINVAL;
  1497. map->lock(map->lock_arg);
  1498. map->async = true;
  1499. ret = _regmap_write(map, reg, val);
  1500. map->async = false;
  1501. map->unlock(map->lock_arg);
  1502. return ret;
  1503. }
  1504. EXPORT_SYMBOL_GPL(regmap_write_async);
  1505. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  1506. const void *val, size_t val_len)
  1507. {
  1508. size_t val_bytes = map->format.val_bytes;
  1509. size_t val_count = val_len / val_bytes;
  1510. size_t chunk_count, chunk_bytes;
  1511. size_t chunk_regs = val_count;
  1512. int ret, i;
  1513. if (!val_count)
  1514. return -EINVAL;
  1515. if (map->use_single_write)
  1516. chunk_regs = 1;
  1517. else if (map->max_raw_write && val_len > map->max_raw_write)
  1518. chunk_regs = map->max_raw_write / val_bytes;
  1519. chunk_count = val_count / chunk_regs;
  1520. chunk_bytes = chunk_regs * val_bytes;
  1521. /* Write as many bytes as possible with chunk_size */
  1522. for (i = 0; i < chunk_count; i++) {
  1523. ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes);
  1524. if (ret)
  1525. return ret;
  1526. reg += regmap_get_offset(map, chunk_regs);
  1527. val += chunk_bytes;
  1528. val_len -= chunk_bytes;
  1529. }
  1530. /* Write remaining bytes */
  1531. if (val_len)
  1532. ret = _regmap_raw_write_impl(map, reg, val, val_len);
  1533. return ret;
  1534. }
  1535. /**
  1536. * regmap_raw_write() - Write raw values to one or more registers
  1537. *
  1538. * @map: Register map to write to
  1539. * @reg: Initial register to write to
  1540. * @val: Block of data to be written, laid out for direct transmission to the
  1541. * device
  1542. * @val_len: Length of data pointed to by val.
  1543. *
  1544. * This function is intended to be used for things like firmware
  1545. * download where a large block of data needs to be transferred to the
  1546. * device. No formatting will be done on the data provided.
  1547. *
  1548. * A value of zero will be returned on success, a negative errno will
  1549. * be returned in error cases.
  1550. */
  1551. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1552. const void *val, size_t val_len)
  1553. {
  1554. int ret;
  1555. if (!regmap_can_raw_write(map))
  1556. return -EINVAL;
  1557. if (val_len % map->format.val_bytes)
  1558. return -EINVAL;
  1559. map->lock(map->lock_arg);
  1560. ret = _regmap_raw_write(map, reg, val, val_len);
  1561. map->unlock(map->lock_arg);
  1562. return ret;
  1563. }
  1564. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1565. /**
  1566. * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
  1567. * register field.
  1568. *
  1569. * @field: Register field to write to
  1570. * @mask: Bitmask to change
  1571. * @val: Value to be written
  1572. * @change: Boolean indicating if a write was done
  1573. * @async: Boolean indicating asynchronously
  1574. * @force: Boolean indicating use force update
  1575. *
  1576. * Perform a read/modify/write cycle on the register field with change,
  1577. * async, force option.
  1578. *
  1579. * A value of zero will be returned on success, a negative errno will
  1580. * be returned in error cases.
  1581. */
  1582. int regmap_field_update_bits_base(struct regmap_field *field,
  1583. unsigned int mask, unsigned int val,
  1584. bool *change, bool async, bool force)
  1585. {
  1586. mask = (mask << field->shift) & field->mask;
  1587. return regmap_update_bits_base(field->regmap, field->reg,
  1588. mask, val << field->shift,
  1589. change, async, force);
  1590. }
  1591. EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
  1592. /**
  1593. * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
  1594. * register field with port ID
  1595. *
  1596. * @field: Register field to write to
  1597. * @id: port ID
  1598. * @mask: Bitmask to change
  1599. * @val: Value to be written
  1600. * @change: Boolean indicating if a write was done
  1601. * @async: Boolean indicating asynchronously
  1602. * @force: Boolean indicating use force update
  1603. *
  1604. * A value of zero will be returned on success, a negative errno will
  1605. * be returned in error cases.
  1606. */
  1607. int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
  1608. unsigned int mask, unsigned int val,
  1609. bool *change, bool async, bool force)
  1610. {
  1611. if (id >= field->id_size)
  1612. return -EINVAL;
  1613. mask = (mask << field->shift) & field->mask;
  1614. return regmap_update_bits_base(field->regmap,
  1615. field->reg + (field->id_offset * id),
  1616. mask, val << field->shift,
  1617. change, async, force);
  1618. }
  1619. EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
  1620. /**
  1621. * regmap_bulk_write() - Write multiple registers to the device
  1622. *
  1623. * @map: Register map to write to
  1624. * @reg: First register to be write from
  1625. * @val: Block of data to be written, in native register size for device
  1626. * @val_count: Number of registers to write
  1627. *
  1628. * This function is intended to be used for writing a large block of
  1629. * data to the device either in single transfer or multiple transfer.
  1630. *
  1631. * A value of zero will be returned on success, a negative errno will
  1632. * be returned in error cases.
  1633. */
  1634. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1635. size_t val_count)
  1636. {
  1637. int ret = 0, i;
  1638. size_t val_bytes = map->format.val_bytes;
  1639. if (!IS_ALIGNED(reg, map->reg_stride))
  1640. return -EINVAL;
  1641. /*
  1642. * Some devices don't support bulk write, for them we have a series of
  1643. * single write operations.
  1644. */
  1645. if (!map->bus || !map->format.parse_inplace) {
  1646. map->lock(map->lock_arg);
  1647. for (i = 0; i < val_count; i++) {
  1648. unsigned int ival;
  1649. switch (val_bytes) {
  1650. case 1:
  1651. ival = *(u8 *)(val + (i * val_bytes));
  1652. break;
  1653. case 2:
  1654. ival = *(u16 *)(val + (i * val_bytes));
  1655. break;
  1656. case 4:
  1657. ival = *(u32 *)(val + (i * val_bytes));
  1658. break;
  1659. #ifdef CONFIG_64BIT
  1660. case 8:
  1661. ival = *(u64 *)(val + (i * val_bytes));
  1662. break;
  1663. #endif
  1664. default:
  1665. ret = -EINVAL;
  1666. goto out;
  1667. }
  1668. ret = _regmap_write(map,
  1669. reg + regmap_get_offset(map, i),
  1670. ival);
  1671. if (ret != 0)
  1672. goto out;
  1673. }
  1674. out:
  1675. map->unlock(map->lock_arg);
  1676. } else {
  1677. void *wval;
  1678. wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
  1679. if (!wval)
  1680. return -ENOMEM;
  1681. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1682. map->format.parse_inplace(wval + i);
  1683. ret = regmap_raw_write(map, reg, wval, val_bytes * val_count);
  1684. kfree(wval);
  1685. }
  1686. return ret;
  1687. }
  1688. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1689. /*
  1690. * _regmap_raw_multi_reg_write()
  1691. *
  1692. * the (register,newvalue) pairs in regs have not been formatted, but
  1693. * they are all in the same page and have been changed to being page
  1694. * relative. The page register has been written if that was necessary.
  1695. */
  1696. static int _regmap_raw_multi_reg_write(struct regmap *map,
  1697. const struct reg_sequence *regs,
  1698. size_t num_regs)
  1699. {
  1700. int ret;
  1701. void *buf;
  1702. int i;
  1703. u8 *u8;
  1704. size_t val_bytes = map->format.val_bytes;
  1705. size_t reg_bytes = map->format.reg_bytes;
  1706. size_t pad_bytes = map->format.pad_bytes;
  1707. size_t pair_size = reg_bytes + pad_bytes + val_bytes;
  1708. size_t len = pair_size * num_regs;
  1709. if (!len)
  1710. return -EINVAL;
  1711. buf = kzalloc(len, GFP_KERNEL);
  1712. if (!buf)
  1713. return -ENOMEM;
  1714. /* We have to linearise by hand. */
  1715. u8 = buf;
  1716. for (i = 0; i < num_regs; i++) {
  1717. unsigned int reg = regs[i].reg;
  1718. unsigned int val = regs[i].def;
  1719. trace_regmap_hw_write_start(map, reg, 1);
  1720. map->format.format_reg(u8, reg, map->reg_shift);
  1721. u8 += reg_bytes + pad_bytes;
  1722. map->format.format_val(u8, val, 0);
  1723. u8 += val_bytes;
  1724. }
  1725. u8 = buf;
  1726. *u8 |= map->write_flag_mask;
  1727. ret = map->bus->write(map->bus_context, buf, len);
  1728. kfree(buf);
  1729. for (i = 0; i < num_regs; i++) {
  1730. int reg = regs[i].reg;
  1731. trace_regmap_hw_write_done(map, reg, 1);
  1732. }
  1733. return ret;
  1734. }
  1735. static unsigned int _regmap_register_page(struct regmap *map,
  1736. unsigned int reg,
  1737. struct regmap_range_node *range)
  1738. {
  1739. unsigned int win_page = (reg - range->range_min) / range->window_len;
  1740. return win_page;
  1741. }
  1742. static int _regmap_range_multi_paged_reg_write(struct regmap *map,
  1743. struct reg_sequence *regs,
  1744. size_t num_regs)
  1745. {
  1746. int ret;
  1747. int i, n;
  1748. struct reg_sequence *base;
  1749. unsigned int this_page = 0;
  1750. unsigned int page_change = 0;
  1751. /*
  1752. * the set of registers are not neccessarily in order, but
  1753. * since the order of write must be preserved this algorithm
  1754. * chops the set each time the page changes. This also applies
  1755. * if there is a delay required at any point in the sequence.
  1756. */
  1757. base = regs;
  1758. for (i = 0, n = 0; i < num_regs; i++, n++) {
  1759. unsigned int reg = regs[i].reg;
  1760. struct regmap_range_node *range;
  1761. range = _regmap_range_lookup(map, reg);
  1762. if (range) {
  1763. unsigned int win_page = _regmap_register_page(map, reg,
  1764. range);
  1765. if (i == 0)
  1766. this_page = win_page;
  1767. if (win_page != this_page) {
  1768. this_page = win_page;
  1769. page_change = 1;
  1770. }
  1771. }
  1772. /* If we have both a page change and a delay make sure to
  1773. * write the regs and apply the delay before we change the
  1774. * page.
  1775. */
  1776. if (page_change || regs[i].delay_us) {
  1777. /* For situations where the first write requires
  1778. * a delay we need to make sure we don't call
  1779. * raw_multi_reg_write with n=0
  1780. * This can't occur with page breaks as we
  1781. * never write on the first iteration
  1782. */
  1783. if (regs[i].delay_us && i == 0)
  1784. n = 1;
  1785. ret = _regmap_raw_multi_reg_write(map, base, n);
  1786. if (ret != 0)
  1787. return ret;
  1788. if (regs[i].delay_us)
  1789. udelay(regs[i].delay_us);
  1790. base += n;
  1791. n = 0;
  1792. if (page_change) {
  1793. ret = _regmap_select_page(map,
  1794. &base[n].reg,
  1795. range, 1);
  1796. if (ret != 0)
  1797. return ret;
  1798. page_change = 0;
  1799. }
  1800. }
  1801. }
  1802. if (n > 0)
  1803. return _regmap_raw_multi_reg_write(map, base, n);
  1804. return 0;
  1805. }
  1806. static int _regmap_multi_reg_write(struct regmap *map,
  1807. const struct reg_sequence *regs,
  1808. size_t num_regs)
  1809. {
  1810. int i;
  1811. int ret;
  1812. if (!map->can_multi_write) {
  1813. for (i = 0; i < num_regs; i++) {
  1814. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1815. if (ret != 0)
  1816. return ret;
  1817. if (regs[i].delay_us)
  1818. udelay(regs[i].delay_us);
  1819. }
  1820. return 0;
  1821. }
  1822. if (!map->format.parse_inplace)
  1823. return -EINVAL;
  1824. if (map->writeable_reg)
  1825. for (i = 0; i < num_regs; i++) {
  1826. int reg = regs[i].reg;
  1827. if (!map->writeable_reg(map->dev, reg))
  1828. return -EINVAL;
  1829. if (!IS_ALIGNED(reg, map->reg_stride))
  1830. return -EINVAL;
  1831. }
  1832. if (!map->cache_bypass) {
  1833. for (i = 0; i < num_regs; i++) {
  1834. unsigned int val = regs[i].def;
  1835. unsigned int reg = regs[i].reg;
  1836. ret = regcache_write(map, reg, val);
  1837. if (ret) {
  1838. dev_err(map->dev,
  1839. "Error in caching of register: %x ret: %d\n",
  1840. reg, ret);
  1841. return ret;
  1842. }
  1843. }
  1844. if (map->cache_only) {
  1845. map->cache_dirty = true;
  1846. return 0;
  1847. }
  1848. }
  1849. WARN_ON(!map->bus);
  1850. for (i = 0; i < num_regs; i++) {
  1851. unsigned int reg = regs[i].reg;
  1852. struct regmap_range_node *range;
  1853. /* Coalesce all the writes between a page break or a delay
  1854. * in a sequence
  1855. */
  1856. range = _regmap_range_lookup(map, reg);
  1857. if (range || regs[i].delay_us) {
  1858. size_t len = sizeof(struct reg_sequence)*num_regs;
  1859. struct reg_sequence *base = kmemdup(regs, len,
  1860. GFP_KERNEL);
  1861. if (!base)
  1862. return -ENOMEM;
  1863. ret = _regmap_range_multi_paged_reg_write(map, base,
  1864. num_regs);
  1865. kfree(base);
  1866. return ret;
  1867. }
  1868. }
  1869. return _regmap_raw_multi_reg_write(map, regs, num_regs);
  1870. }
  1871. /**
  1872. * regmap_multi_reg_write() - Write multiple registers to the device
  1873. *
  1874. * @map: Register map to write to
  1875. * @regs: Array of structures containing register,value to be written
  1876. * @num_regs: Number of registers to write
  1877. *
  1878. * Write multiple registers to the device where the set of register, value
  1879. * pairs are supplied in any order, possibly not all in a single range.
  1880. *
  1881. * The 'normal' block write mode will send ultimately send data on the
  1882. * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
  1883. * addressed. However, this alternative block multi write mode will send
  1884. * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
  1885. * must of course support the mode.
  1886. *
  1887. * A value of zero will be returned on success, a negative errno will be
  1888. * returned in error cases.
  1889. */
  1890. int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
  1891. int num_regs)
  1892. {
  1893. int ret;
  1894. map->lock(map->lock_arg);
  1895. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1896. map->unlock(map->lock_arg);
  1897. return ret;
  1898. }
  1899. EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
  1900. /**
  1901. * regmap_multi_reg_write_bypassed() - Write multiple registers to the
  1902. * device but not the cache
  1903. *
  1904. * @map: Register map to write to
  1905. * @regs: Array of structures containing register,value to be written
  1906. * @num_regs: Number of registers to write
  1907. *
  1908. * Write multiple registers to the device but not the cache where the set
  1909. * of register are supplied in any order.
  1910. *
  1911. * This function is intended to be used for writing a large block of data
  1912. * atomically to the device in single transfer for those I2C client devices
  1913. * that implement this alternative block write mode.
  1914. *
  1915. * A value of zero will be returned on success, a negative errno will
  1916. * be returned in error cases.
  1917. */
  1918. int regmap_multi_reg_write_bypassed(struct regmap *map,
  1919. const struct reg_sequence *regs,
  1920. int num_regs)
  1921. {
  1922. int ret;
  1923. bool bypass;
  1924. map->lock(map->lock_arg);
  1925. bypass = map->cache_bypass;
  1926. map->cache_bypass = true;
  1927. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1928. map->cache_bypass = bypass;
  1929. map->unlock(map->lock_arg);
  1930. return ret;
  1931. }
  1932. EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
  1933. /**
  1934. * regmap_raw_write_async() - Write raw values to one or more registers
  1935. * asynchronously
  1936. *
  1937. * @map: Register map to write to
  1938. * @reg: Initial register to write to
  1939. * @val: Block of data to be written, laid out for direct transmission to the
  1940. * device. Must be valid until regmap_async_complete() is called.
  1941. * @val_len: Length of data pointed to by val.
  1942. *
  1943. * This function is intended to be used for things like firmware
  1944. * download where a large block of data needs to be transferred to the
  1945. * device. No formatting will be done on the data provided.
  1946. *
  1947. * If supported by the underlying bus the write will be scheduled
  1948. * asynchronously, helping maximise I/O speed on higher speed buses
  1949. * like SPI. regmap_async_complete() can be called to ensure that all
  1950. * asynchrnous writes have been completed.
  1951. *
  1952. * A value of zero will be returned on success, a negative errno will
  1953. * be returned in error cases.
  1954. */
  1955. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  1956. const void *val, size_t val_len)
  1957. {
  1958. int ret;
  1959. if (val_len % map->format.val_bytes)
  1960. return -EINVAL;
  1961. if (!IS_ALIGNED(reg, map->reg_stride))
  1962. return -EINVAL;
  1963. map->lock(map->lock_arg);
  1964. map->async = true;
  1965. ret = _regmap_raw_write(map, reg, val, val_len);
  1966. map->async = false;
  1967. map->unlock(map->lock_arg);
  1968. return ret;
  1969. }
  1970. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  1971. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1972. unsigned int val_len)
  1973. {
  1974. struct regmap_range_node *range;
  1975. int ret;
  1976. WARN_ON(!map->bus);
  1977. if (!map->bus || !map->bus->read)
  1978. return -EINVAL;
  1979. range = _regmap_range_lookup(map, reg);
  1980. if (range) {
  1981. ret = _regmap_select_page(map, &reg, range,
  1982. val_len / map->format.val_bytes);
  1983. if (ret != 0)
  1984. return ret;
  1985. }
  1986. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1987. regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
  1988. map->read_flag_mask);
  1989. trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
  1990. ret = map->bus->read(map->bus_context, map->work_buf,
  1991. map->format.reg_bytes + map->format.pad_bytes,
  1992. val, val_len);
  1993. trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
  1994. return ret;
  1995. }
  1996. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  1997. unsigned int *val)
  1998. {
  1999. struct regmap *map = context;
  2000. return map->bus->reg_read(map->bus_context, reg, val);
  2001. }
  2002. static int _regmap_bus_read(void *context, unsigned int reg,
  2003. unsigned int *val)
  2004. {
  2005. int ret;
  2006. struct regmap *map = context;
  2007. void *work_val = map->work_buf + map->format.reg_bytes +
  2008. map->format.pad_bytes;
  2009. if (!map->format.parse_val)
  2010. return -EINVAL;
  2011. ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes);
  2012. if (ret == 0)
  2013. *val = map->format.parse_val(work_val);
  2014. return ret;
  2015. }
  2016. static int _regmap_read(struct regmap *map, unsigned int reg,
  2017. unsigned int *val)
  2018. {
  2019. int ret;
  2020. void *context = _regmap_map_get_context(map);
  2021. if (!map->cache_bypass) {
  2022. ret = regcache_read(map, reg, val);
  2023. if (ret == 0)
  2024. return 0;
  2025. }
  2026. if (map->cache_only)
  2027. return -EBUSY;
  2028. if (!regmap_readable(map, reg))
  2029. return -EIO;
  2030. ret = map->reg_read(context, reg, val);
  2031. if (ret == 0) {
  2032. #ifdef LOG_DEVICE
  2033. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  2034. dev_info(map->dev, "%x => %x\n", reg, *val);
  2035. #endif
  2036. trace_regmap_reg_read(map, reg, *val);
  2037. if (!map->cache_bypass)
  2038. regcache_write(map, reg, *val);
  2039. }
  2040. return ret;
  2041. }
  2042. /**
  2043. * regmap_read() - Read a value from a single register
  2044. *
  2045. * @map: Register map to read from
  2046. * @reg: Register to be read from
  2047. * @val: Pointer to store read value
  2048. *
  2049. * A value of zero will be returned on success, a negative errno will
  2050. * be returned in error cases.
  2051. */
  2052. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  2053. {
  2054. int ret;
  2055. if (!IS_ALIGNED(reg, map->reg_stride))
  2056. return -EINVAL;
  2057. map->lock(map->lock_arg);
  2058. ret = _regmap_read(map, reg, val);
  2059. map->unlock(map->lock_arg);
  2060. return ret;
  2061. }
  2062. EXPORT_SYMBOL_GPL(regmap_read);
  2063. /**
  2064. * regmap_raw_read() - Read raw data from the device
  2065. *
  2066. * @map: Register map to read from
  2067. * @reg: First register to be read from
  2068. * @val: Pointer to store read value
  2069. * @val_len: Size of data to read
  2070. *
  2071. * A value of zero will be returned on success, a negative errno will
  2072. * be returned in error cases.
  2073. */
  2074. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  2075. size_t val_len)
  2076. {
  2077. size_t val_bytes = map->format.val_bytes;
  2078. size_t val_count = val_len / val_bytes;
  2079. unsigned int v;
  2080. int ret, i;
  2081. if (!map->bus)
  2082. return -EINVAL;
  2083. if (val_len % map->format.val_bytes)
  2084. return -EINVAL;
  2085. if (!IS_ALIGNED(reg, map->reg_stride))
  2086. return -EINVAL;
  2087. if (val_count == 0)
  2088. return -EINVAL;
  2089. map->lock(map->lock_arg);
  2090. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  2091. map->cache_type == REGCACHE_NONE) {
  2092. size_t chunk_count, chunk_bytes;
  2093. size_t chunk_regs = val_count;
  2094. if (!map->bus->read) {
  2095. ret = -ENOTSUPP;
  2096. goto out;
  2097. }
  2098. if (map->use_single_read)
  2099. chunk_regs = 1;
  2100. else if (map->max_raw_read && val_len > map->max_raw_read)
  2101. chunk_regs = map->max_raw_read / val_bytes;
  2102. chunk_count = val_count / chunk_regs;
  2103. chunk_bytes = chunk_regs * val_bytes;
  2104. /* Read bytes that fit into whole chunks */
  2105. for (i = 0; i < chunk_count; i++) {
  2106. ret = _regmap_raw_read(map, reg, val, chunk_bytes);
  2107. if (ret != 0)
  2108. goto out;
  2109. reg += regmap_get_offset(map, chunk_regs);
  2110. val += chunk_bytes;
  2111. val_len -= chunk_bytes;
  2112. }
  2113. /* Read remaining bytes */
  2114. if (val_len) {
  2115. ret = _regmap_raw_read(map, reg, val, val_len);
  2116. if (ret != 0)
  2117. goto out;
  2118. }
  2119. } else {
  2120. /* Otherwise go word by word for the cache; should be low
  2121. * cost as we expect to hit the cache.
  2122. */
  2123. for (i = 0; i < val_count; i++) {
  2124. ret = _regmap_read(map, reg + regmap_get_offset(map, i),
  2125. &v);
  2126. if (ret != 0)
  2127. goto out;
  2128. map->format.format_val(val + (i * val_bytes), v, 0);
  2129. }
  2130. }
  2131. out:
  2132. map->unlock(map->lock_arg);
  2133. return ret;
  2134. }
  2135. EXPORT_SYMBOL_GPL(regmap_raw_read);
  2136. /**
  2137. * regmap_field_read() - Read a value to a single register field
  2138. *
  2139. * @field: Register field to read from
  2140. * @val: Pointer to store read value
  2141. *
  2142. * A value of zero will be returned on success, a negative errno will
  2143. * be returned in error cases.
  2144. */
  2145. int regmap_field_read(struct regmap_field *field, unsigned int *val)
  2146. {
  2147. int ret;
  2148. unsigned int reg_val;
  2149. ret = regmap_read(field->regmap, field->reg, &reg_val);
  2150. if (ret != 0)
  2151. return ret;
  2152. reg_val &= field->mask;
  2153. reg_val >>= field->shift;
  2154. *val = reg_val;
  2155. return ret;
  2156. }
  2157. EXPORT_SYMBOL_GPL(regmap_field_read);
  2158. /**
  2159. * regmap_fields_read() - Read a value to a single register field with port ID
  2160. *
  2161. * @field: Register field to read from
  2162. * @id: port ID
  2163. * @val: Pointer to store read value
  2164. *
  2165. * A value of zero will be returned on success, a negative errno will
  2166. * be returned in error cases.
  2167. */
  2168. int regmap_fields_read(struct regmap_field *field, unsigned int id,
  2169. unsigned int *val)
  2170. {
  2171. int ret;
  2172. unsigned int reg_val;
  2173. if (id >= field->id_size)
  2174. return -EINVAL;
  2175. ret = regmap_read(field->regmap,
  2176. field->reg + (field->id_offset * id),
  2177. &reg_val);
  2178. if (ret != 0)
  2179. return ret;
  2180. reg_val &= field->mask;
  2181. reg_val >>= field->shift;
  2182. *val = reg_val;
  2183. return ret;
  2184. }
  2185. EXPORT_SYMBOL_GPL(regmap_fields_read);
  2186. /**
  2187. * regmap_bulk_read() - Read multiple registers from the device
  2188. *
  2189. * @map: Register map to read from
  2190. * @reg: First register to be read from
  2191. * @val: Pointer to store read value, in native register size for device
  2192. * @val_count: Number of registers to read
  2193. *
  2194. * A value of zero will be returned on success, a negative errno will
  2195. * be returned in error cases.
  2196. */
  2197. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  2198. size_t val_count)
  2199. {
  2200. int ret, i;
  2201. size_t val_bytes = map->format.val_bytes;
  2202. bool vol = regmap_volatile_range(map, reg, val_count);
  2203. if (!IS_ALIGNED(reg, map->reg_stride))
  2204. return -EINVAL;
  2205. if (val_count == 0)
  2206. return -EINVAL;
  2207. if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
  2208. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  2209. if (ret != 0)
  2210. return ret;
  2211. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  2212. map->format.parse_inplace(val + i);
  2213. } else {
  2214. #ifdef CONFIG_64BIT
  2215. u64 *u64 = val;
  2216. #endif
  2217. u32 *u32 = val;
  2218. u16 *u16 = val;
  2219. u8 *u8 = val;
  2220. map->lock(map->lock_arg);
  2221. for (i = 0; i < val_count; i++) {
  2222. unsigned int ival;
  2223. ret = _regmap_read(map, reg + regmap_get_offset(map, i),
  2224. &ival);
  2225. if (ret != 0)
  2226. goto out;
  2227. switch (map->format.val_bytes) {
  2228. #ifdef CONFIG_64BIT
  2229. case 8:
  2230. u64[i] = ival;
  2231. break;
  2232. #endif
  2233. case 4:
  2234. u32[i] = ival;
  2235. break;
  2236. case 2:
  2237. u16[i] = ival;
  2238. break;
  2239. case 1:
  2240. u8[i] = ival;
  2241. break;
  2242. default:
  2243. ret = -EINVAL;
  2244. goto out;
  2245. }
  2246. }
  2247. out:
  2248. map->unlock(map->lock_arg);
  2249. }
  2250. return ret;
  2251. }
  2252. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  2253. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  2254. unsigned int mask, unsigned int val,
  2255. bool *change, bool force_write)
  2256. {
  2257. int ret;
  2258. unsigned int tmp, orig;
  2259. if (change)
  2260. *change = false;
  2261. if (regmap_volatile(map, reg) && map->reg_update_bits) {
  2262. ret = map->reg_update_bits(map->bus_context, reg, mask, val);
  2263. if (ret == 0 && change)
  2264. *change = true;
  2265. } else {
  2266. ret = _regmap_read(map, reg, &orig);
  2267. if (ret != 0)
  2268. return ret;
  2269. tmp = orig & ~mask;
  2270. tmp |= val & mask;
  2271. if (force_write || (tmp != orig)) {
  2272. ret = _regmap_write(map, reg, tmp);
  2273. if (ret == 0 && change)
  2274. *change = true;
  2275. }
  2276. }
  2277. return ret;
  2278. }
  2279. /**
  2280. * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
  2281. *
  2282. * @map: Register map to update
  2283. * @reg: Register to update
  2284. * @mask: Bitmask to change
  2285. * @val: New value for bitmask
  2286. * @change: Boolean indicating if a write was done
  2287. * @async: Boolean indicating asynchronously
  2288. * @force: Boolean indicating use force update
  2289. *
  2290. * Perform a read/modify/write cycle on a register map with change, async, force
  2291. * options.
  2292. *
  2293. * If async is true:
  2294. *
  2295. * With most buses the read must be done synchronously so this is most useful
  2296. * for devices with a cache which do not need to interact with the hardware to
  2297. * determine the current register value.
  2298. *
  2299. * Returns zero for success, a negative number on error.
  2300. */
  2301. int regmap_update_bits_base(struct regmap *map, unsigned int reg,
  2302. unsigned int mask, unsigned int val,
  2303. bool *change, bool async, bool force)
  2304. {
  2305. int ret;
  2306. map->lock(map->lock_arg);
  2307. map->async = async;
  2308. ret = _regmap_update_bits(map, reg, mask, val, change, force);
  2309. map->async = false;
  2310. map->unlock(map->lock_arg);
  2311. return ret;
  2312. }
  2313. EXPORT_SYMBOL_GPL(regmap_update_bits_base);
  2314. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  2315. {
  2316. struct regmap *map = async->map;
  2317. bool wake;
  2318. trace_regmap_async_io_complete(map);
  2319. spin_lock(&map->async_lock);
  2320. list_move(&async->list, &map->async_free);
  2321. wake = list_empty(&map->async_list);
  2322. if (ret != 0)
  2323. map->async_ret = ret;
  2324. spin_unlock(&map->async_lock);
  2325. if (wake)
  2326. wake_up(&map->async_waitq);
  2327. }
  2328. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  2329. static int regmap_async_is_done(struct regmap *map)
  2330. {
  2331. unsigned long flags;
  2332. int ret;
  2333. spin_lock_irqsave(&map->async_lock, flags);
  2334. ret = list_empty(&map->async_list);
  2335. spin_unlock_irqrestore(&map->async_lock, flags);
  2336. return ret;
  2337. }
  2338. /**
  2339. * regmap_async_complete - Ensure all asynchronous I/O has completed.
  2340. *
  2341. * @map: Map to operate on.
  2342. *
  2343. * Blocks until any pending asynchronous I/O has completed. Returns
  2344. * an error code for any failed I/O operations.
  2345. */
  2346. int regmap_async_complete(struct regmap *map)
  2347. {
  2348. unsigned long flags;
  2349. int ret;
  2350. /* Nothing to do with no async support */
  2351. if (!map->bus || !map->bus->async_write)
  2352. return 0;
  2353. trace_regmap_async_complete_start(map);
  2354. wait_event(map->async_waitq, regmap_async_is_done(map));
  2355. spin_lock_irqsave(&map->async_lock, flags);
  2356. ret = map->async_ret;
  2357. map->async_ret = 0;
  2358. spin_unlock_irqrestore(&map->async_lock, flags);
  2359. trace_regmap_async_complete_done(map);
  2360. return ret;
  2361. }
  2362. EXPORT_SYMBOL_GPL(regmap_async_complete);
  2363. /**
  2364. * regmap_register_patch - Register and apply register updates to be applied
  2365. * on device initialistion
  2366. *
  2367. * @map: Register map to apply updates to.
  2368. * @regs: Values to update.
  2369. * @num_regs: Number of entries in regs.
  2370. *
  2371. * Register a set of register updates to be applied to the device
  2372. * whenever the device registers are synchronised with the cache and
  2373. * apply them immediately. Typically this is used to apply
  2374. * corrections to be applied to the device defaults on startup, such
  2375. * as the updates some vendors provide to undocumented registers.
  2376. *
  2377. * The caller must ensure that this function cannot be called
  2378. * concurrently with either itself or regcache_sync().
  2379. */
  2380. int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
  2381. int num_regs)
  2382. {
  2383. struct reg_sequence *p;
  2384. int ret;
  2385. bool bypass;
  2386. if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
  2387. num_regs))
  2388. return 0;
  2389. p = krealloc(map->patch,
  2390. sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
  2391. GFP_KERNEL);
  2392. if (p) {
  2393. memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
  2394. map->patch = p;
  2395. map->patch_regs += num_regs;
  2396. } else {
  2397. return -ENOMEM;
  2398. }
  2399. map->lock(map->lock_arg);
  2400. bypass = map->cache_bypass;
  2401. map->cache_bypass = true;
  2402. map->async = true;
  2403. ret = _regmap_multi_reg_write(map, regs, num_regs);
  2404. map->async = false;
  2405. map->cache_bypass = bypass;
  2406. map->unlock(map->lock_arg);
  2407. regmap_async_complete(map);
  2408. return ret;
  2409. }
  2410. EXPORT_SYMBOL_GPL(regmap_register_patch);
  2411. /**
  2412. * regmap_get_val_bytes() - Report the size of a register value
  2413. *
  2414. * @map: Register map to operate on.
  2415. *
  2416. * Report the size of a register value, mainly intended to for use by
  2417. * generic infrastructure built on top of regmap.
  2418. */
  2419. int regmap_get_val_bytes(struct regmap *map)
  2420. {
  2421. if (map->format.format_write)
  2422. return -EINVAL;
  2423. return map->format.val_bytes;
  2424. }
  2425. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  2426. /**
  2427. * regmap_get_max_register() - Report the max register value
  2428. *
  2429. * @map: Register map to operate on.
  2430. *
  2431. * Report the max register value, mainly intended to for use by
  2432. * generic infrastructure built on top of regmap.
  2433. */
  2434. int regmap_get_max_register(struct regmap *map)
  2435. {
  2436. return map->max_register ? map->max_register : -EINVAL;
  2437. }
  2438. EXPORT_SYMBOL_GPL(regmap_get_max_register);
  2439. /**
  2440. * regmap_get_reg_stride() - Report the register address stride
  2441. *
  2442. * @map: Register map to operate on.
  2443. *
  2444. * Report the register address stride, mainly intended to for use by
  2445. * generic infrastructure built on top of regmap.
  2446. */
  2447. int regmap_get_reg_stride(struct regmap *map)
  2448. {
  2449. return map->reg_stride;
  2450. }
  2451. EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
  2452. int regmap_parse_val(struct regmap *map, const void *buf,
  2453. unsigned int *val)
  2454. {
  2455. if (!map->format.parse_val)
  2456. return -EINVAL;
  2457. *val = map->format.parse_val(buf);
  2458. return 0;
  2459. }
  2460. EXPORT_SYMBOL_GPL(regmap_parse_val);
  2461. static int __init regmap_initcall(void)
  2462. {
  2463. regmap_debugfs_initcall();
  2464. return 0;
  2465. }
  2466. postcore_initcall(regmap_initcall);