regmap.c 69 KB

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