regmap.c 72 KB

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