regmap.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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/rbtree.h>
  18. #include <linux/sched.h>
  19. #define CREATE_TRACE_POINTS
  20. #include <trace/events/regmap.h>
  21. #include "internal.h"
  22. /*
  23. * Sometimes for failures during very early init the trace
  24. * infrastructure isn't available early enough to be used. For this
  25. * sort of problem defining LOG_DEVICE will add printks for basic
  26. * register I/O on a specific device.
  27. */
  28. #undef LOG_DEVICE
  29. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  30. unsigned int mask, unsigned int val,
  31. bool *change);
  32. static int _regmap_bus_read(void *context, unsigned int reg,
  33. unsigned int *val);
  34. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  35. unsigned int val);
  36. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  37. unsigned int val);
  38. static void async_cleanup(struct work_struct *work)
  39. {
  40. struct regmap_async *async = container_of(work, struct regmap_async,
  41. cleanup);
  42. kfree(async->work_buf);
  43. kfree(async);
  44. }
  45. bool regmap_reg_in_ranges(unsigned int reg,
  46. const struct regmap_range *ranges,
  47. unsigned int nranges)
  48. {
  49. const struct regmap_range *r;
  50. int i;
  51. for (i = 0, r = ranges; i < nranges; i++, r++)
  52. if (regmap_reg_in_range(reg, r))
  53. return true;
  54. return false;
  55. }
  56. EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
  57. bool regmap_check_range_table(struct regmap *map, unsigned int reg,
  58. const struct regmap_access_table *table)
  59. {
  60. /* Check "no ranges" first */
  61. if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
  62. return false;
  63. /* In case zero "yes ranges" are supplied, any reg is OK */
  64. if (!table->n_yes_ranges)
  65. return true;
  66. return regmap_reg_in_ranges(reg, table->yes_ranges,
  67. table->n_yes_ranges);
  68. }
  69. EXPORT_SYMBOL_GPL(regmap_check_range_table);
  70. bool regmap_writeable(struct regmap *map, unsigned int reg)
  71. {
  72. if (map->max_register && reg > map->max_register)
  73. return false;
  74. if (map->writeable_reg)
  75. return map->writeable_reg(map->dev, reg);
  76. if (map->wr_table)
  77. return regmap_check_range_table(map, reg, map->wr_table);
  78. return true;
  79. }
  80. bool regmap_readable(struct regmap *map, unsigned int reg)
  81. {
  82. if (map->max_register && reg > map->max_register)
  83. return false;
  84. if (map->format.format_write)
  85. return false;
  86. if (map->readable_reg)
  87. return map->readable_reg(map->dev, reg);
  88. if (map->rd_table)
  89. return regmap_check_range_table(map, reg, map->rd_table);
  90. return true;
  91. }
  92. bool regmap_volatile(struct regmap *map, unsigned int reg)
  93. {
  94. if (!regmap_readable(map, reg))
  95. return false;
  96. if (map->volatile_reg)
  97. return map->volatile_reg(map->dev, reg);
  98. if (map->volatile_table)
  99. return regmap_check_range_table(map, reg, map->volatile_table);
  100. if (map->cache_ops)
  101. return false;
  102. else
  103. return true;
  104. }
  105. bool regmap_precious(struct regmap *map, unsigned int reg)
  106. {
  107. if (!regmap_readable(map, reg))
  108. return false;
  109. if (map->precious_reg)
  110. return map->precious_reg(map->dev, reg);
  111. if (map->precious_table)
  112. return regmap_check_range_table(map, reg, map->precious_table);
  113. return false;
  114. }
  115. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  116. size_t num)
  117. {
  118. unsigned int i;
  119. for (i = 0; i < num; i++)
  120. if (!regmap_volatile(map, reg + i))
  121. return false;
  122. return true;
  123. }
  124. static void regmap_format_2_6_write(struct regmap *map,
  125. unsigned int reg, unsigned int val)
  126. {
  127. u8 *out = map->work_buf;
  128. *out = (reg << 6) | val;
  129. }
  130. static void regmap_format_4_12_write(struct regmap *map,
  131. unsigned int reg, unsigned int val)
  132. {
  133. __be16 *out = map->work_buf;
  134. *out = cpu_to_be16((reg << 12) | val);
  135. }
  136. static void regmap_format_7_9_write(struct regmap *map,
  137. unsigned int reg, unsigned int val)
  138. {
  139. __be16 *out = map->work_buf;
  140. *out = cpu_to_be16((reg << 9) | val);
  141. }
  142. static void regmap_format_10_14_write(struct regmap *map,
  143. unsigned int reg, unsigned int val)
  144. {
  145. u8 *out = map->work_buf;
  146. out[2] = val;
  147. out[1] = (val >> 8) | (reg << 6);
  148. out[0] = reg >> 2;
  149. }
  150. static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
  151. {
  152. u8 *b = buf;
  153. b[0] = val << shift;
  154. }
  155. static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
  156. {
  157. __be16 *b = buf;
  158. b[0] = cpu_to_be16(val << shift);
  159. }
  160. static void regmap_format_16_native(void *buf, unsigned int val,
  161. unsigned int shift)
  162. {
  163. *(u16 *)buf = val << shift;
  164. }
  165. static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
  166. {
  167. u8 *b = buf;
  168. val <<= shift;
  169. b[0] = val >> 16;
  170. b[1] = val >> 8;
  171. b[2] = val;
  172. }
  173. static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
  174. {
  175. __be32 *b = buf;
  176. b[0] = cpu_to_be32(val << shift);
  177. }
  178. static void regmap_format_32_native(void *buf, unsigned int val,
  179. unsigned int shift)
  180. {
  181. *(u32 *)buf = val << shift;
  182. }
  183. static void regmap_parse_inplace_noop(void *buf)
  184. {
  185. }
  186. static unsigned int regmap_parse_8(const void *buf)
  187. {
  188. const u8 *b = buf;
  189. return b[0];
  190. }
  191. static unsigned int regmap_parse_16_be(const void *buf)
  192. {
  193. const __be16 *b = buf;
  194. return be16_to_cpu(b[0]);
  195. }
  196. static void regmap_parse_16_be_inplace(void *buf)
  197. {
  198. __be16 *b = buf;
  199. b[0] = be16_to_cpu(b[0]);
  200. }
  201. static unsigned int regmap_parse_16_native(const void *buf)
  202. {
  203. return *(u16 *)buf;
  204. }
  205. static unsigned int regmap_parse_24(const void *buf)
  206. {
  207. const u8 *b = buf;
  208. unsigned int ret = b[2];
  209. ret |= ((unsigned int)b[1]) << 8;
  210. ret |= ((unsigned int)b[0]) << 16;
  211. return ret;
  212. }
  213. static unsigned int regmap_parse_32_be(const void *buf)
  214. {
  215. const __be32 *b = buf;
  216. return be32_to_cpu(b[0]);
  217. }
  218. static void regmap_parse_32_be_inplace(void *buf)
  219. {
  220. __be32 *b = buf;
  221. b[0] = be32_to_cpu(b[0]);
  222. }
  223. static unsigned int regmap_parse_32_native(const void *buf)
  224. {
  225. return *(u32 *)buf;
  226. }
  227. static void regmap_lock_mutex(void *__map)
  228. {
  229. struct regmap *map = __map;
  230. mutex_lock(&map->mutex);
  231. }
  232. static void regmap_unlock_mutex(void *__map)
  233. {
  234. struct regmap *map = __map;
  235. mutex_unlock(&map->mutex);
  236. }
  237. static void regmap_lock_spinlock(void *__map)
  238. __acquires(&map->spinlock)
  239. {
  240. struct regmap *map = __map;
  241. unsigned long flags;
  242. spin_lock_irqsave(&map->spinlock, flags);
  243. map->spinlock_flags = flags;
  244. }
  245. static void regmap_unlock_spinlock(void *__map)
  246. __releases(&map->spinlock)
  247. {
  248. struct regmap *map = __map;
  249. spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
  250. }
  251. static void dev_get_regmap_release(struct device *dev, void *res)
  252. {
  253. /*
  254. * We don't actually have anything to do here; the goal here
  255. * is not to manage the regmap but to provide a simple way to
  256. * get the regmap back given a struct device.
  257. */
  258. }
  259. static bool _regmap_range_add(struct regmap *map,
  260. struct regmap_range_node *data)
  261. {
  262. struct rb_root *root = &map->range_tree;
  263. struct rb_node **new = &(root->rb_node), *parent = NULL;
  264. while (*new) {
  265. struct regmap_range_node *this =
  266. container_of(*new, struct regmap_range_node, node);
  267. parent = *new;
  268. if (data->range_max < this->range_min)
  269. new = &((*new)->rb_left);
  270. else if (data->range_min > this->range_max)
  271. new = &((*new)->rb_right);
  272. else
  273. return false;
  274. }
  275. rb_link_node(&data->node, parent, new);
  276. rb_insert_color(&data->node, root);
  277. return true;
  278. }
  279. static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
  280. unsigned int reg)
  281. {
  282. struct rb_node *node = map->range_tree.rb_node;
  283. while (node) {
  284. struct regmap_range_node *this =
  285. container_of(node, struct regmap_range_node, node);
  286. if (reg < this->range_min)
  287. node = node->rb_left;
  288. else if (reg > this->range_max)
  289. node = node->rb_right;
  290. else
  291. return this;
  292. }
  293. return NULL;
  294. }
  295. static void regmap_range_exit(struct regmap *map)
  296. {
  297. struct rb_node *next;
  298. struct regmap_range_node *range_node;
  299. next = rb_first(&map->range_tree);
  300. while (next) {
  301. range_node = rb_entry(next, struct regmap_range_node, node);
  302. next = rb_next(&range_node->node);
  303. rb_erase(&range_node->node, &map->range_tree);
  304. kfree(range_node);
  305. }
  306. kfree(map->selector_work_buf);
  307. }
  308. /**
  309. * regmap_init(): Initialise register map
  310. *
  311. * @dev: Device that will be interacted with
  312. * @bus: Bus-specific callbacks to use with device
  313. * @bus_context: Data passed to bus-specific callbacks
  314. * @config: Configuration for register map
  315. *
  316. * The return value will be an ERR_PTR() on error or a valid pointer to
  317. * a struct regmap. This function should generally not be called
  318. * directly, it should be called by bus-specific init functions.
  319. */
  320. struct regmap *regmap_init(struct device *dev,
  321. const struct regmap_bus *bus,
  322. void *bus_context,
  323. const struct regmap_config *config)
  324. {
  325. struct regmap *map, **m;
  326. int ret = -EINVAL;
  327. enum regmap_endian reg_endian, val_endian;
  328. int i, j;
  329. if (!config)
  330. goto err;
  331. map = kzalloc(sizeof(*map), GFP_KERNEL);
  332. if (map == NULL) {
  333. ret = -ENOMEM;
  334. goto err;
  335. }
  336. if (config->lock && config->unlock) {
  337. map->lock = config->lock;
  338. map->unlock = config->unlock;
  339. map->lock_arg = config->lock_arg;
  340. } else {
  341. if ((bus && bus->fast_io) ||
  342. config->fast_io) {
  343. spin_lock_init(&map->spinlock);
  344. map->lock = regmap_lock_spinlock;
  345. map->unlock = regmap_unlock_spinlock;
  346. } else {
  347. mutex_init(&map->mutex);
  348. map->lock = regmap_lock_mutex;
  349. map->unlock = regmap_unlock_mutex;
  350. }
  351. map->lock_arg = map;
  352. }
  353. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  354. map->format.pad_bytes = config->pad_bits / 8;
  355. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  356. map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
  357. config->val_bits + config->pad_bits, 8);
  358. map->reg_shift = config->pad_bits % 8;
  359. if (config->reg_stride)
  360. map->reg_stride = config->reg_stride;
  361. else
  362. map->reg_stride = 1;
  363. map->use_single_rw = config->use_single_rw;
  364. map->dev = dev;
  365. map->bus = bus;
  366. map->bus_context = bus_context;
  367. map->max_register = config->max_register;
  368. map->wr_table = config->wr_table;
  369. map->rd_table = config->rd_table;
  370. map->volatile_table = config->volatile_table;
  371. map->precious_table = config->precious_table;
  372. map->writeable_reg = config->writeable_reg;
  373. map->readable_reg = config->readable_reg;
  374. map->volatile_reg = config->volatile_reg;
  375. map->precious_reg = config->precious_reg;
  376. map->cache_type = config->cache_type;
  377. map->name = config->name;
  378. spin_lock_init(&map->async_lock);
  379. INIT_LIST_HEAD(&map->async_list);
  380. init_waitqueue_head(&map->async_waitq);
  381. if (config->read_flag_mask || config->write_flag_mask) {
  382. map->read_flag_mask = config->read_flag_mask;
  383. map->write_flag_mask = config->write_flag_mask;
  384. } else if (bus) {
  385. map->read_flag_mask = bus->read_flag_mask;
  386. }
  387. if (!bus) {
  388. map->reg_read = config->reg_read;
  389. map->reg_write = config->reg_write;
  390. map->defer_caching = false;
  391. goto skip_format_initialization;
  392. } else {
  393. map->reg_read = _regmap_bus_read;
  394. }
  395. reg_endian = config->reg_format_endian;
  396. if (reg_endian == REGMAP_ENDIAN_DEFAULT)
  397. reg_endian = bus->reg_format_endian_default;
  398. if (reg_endian == REGMAP_ENDIAN_DEFAULT)
  399. reg_endian = REGMAP_ENDIAN_BIG;
  400. val_endian = config->val_format_endian;
  401. if (val_endian == REGMAP_ENDIAN_DEFAULT)
  402. val_endian = bus->val_format_endian_default;
  403. if (val_endian == REGMAP_ENDIAN_DEFAULT)
  404. val_endian = REGMAP_ENDIAN_BIG;
  405. switch (config->reg_bits + map->reg_shift) {
  406. case 2:
  407. switch (config->val_bits) {
  408. case 6:
  409. map->format.format_write = regmap_format_2_6_write;
  410. break;
  411. default:
  412. goto err_map;
  413. }
  414. break;
  415. case 4:
  416. switch (config->val_bits) {
  417. case 12:
  418. map->format.format_write = regmap_format_4_12_write;
  419. break;
  420. default:
  421. goto err_map;
  422. }
  423. break;
  424. case 7:
  425. switch (config->val_bits) {
  426. case 9:
  427. map->format.format_write = regmap_format_7_9_write;
  428. break;
  429. default:
  430. goto err_map;
  431. }
  432. break;
  433. case 10:
  434. switch (config->val_bits) {
  435. case 14:
  436. map->format.format_write = regmap_format_10_14_write;
  437. break;
  438. default:
  439. goto err_map;
  440. }
  441. break;
  442. case 8:
  443. map->format.format_reg = regmap_format_8;
  444. break;
  445. case 16:
  446. switch (reg_endian) {
  447. case REGMAP_ENDIAN_BIG:
  448. map->format.format_reg = regmap_format_16_be;
  449. break;
  450. case REGMAP_ENDIAN_NATIVE:
  451. map->format.format_reg = regmap_format_16_native;
  452. break;
  453. default:
  454. goto err_map;
  455. }
  456. break;
  457. case 24:
  458. if (reg_endian != REGMAP_ENDIAN_BIG)
  459. goto err_map;
  460. map->format.format_reg = regmap_format_24;
  461. break;
  462. case 32:
  463. switch (reg_endian) {
  464. case REGMAP_ENDIAN_BIG:
  465. map->format.format_reg = regmap_format_32_be;
  466. break;
  467. case REGMAP_ENDIAN_NATIVE:
  468. map->format.format_reg = regmap_format_32_native;
  469. break;
  470. default:
  471. goto err_map;
  472. }
  473. break;
  474. default:
  475. goto err_map;
  476. }
  477. if (val_endian == REGMAP_ENDIAN_NATIVE)
  478. map->format.parse_inplace = regmap_parse_inplace_noop;
  479. switch (config->val_bits) {
  480. case 8:
  481. map->format.format_val = regmap_format_8;
  482. map->format.parse_val = regmap_parse_8;
  483. map->format.parse_inplace = regmap_parse_inplace_noop;
  484. break;
  485. case 16:
  486. switch (val_endian) {
  487. case REGMAP_ENDIAN_BIG:
  488. map->format.format_val = regmap_format_16_be;
  489. map->format.parse_val = regmap_parse_16_be;
  490. map->format.parse_inplace = regmap_parse_16_be_inplace;
  491. break;
  492. case REGMAP_ENDIAN_NATIVE:
  493. map->format.format_val = regmap_format_16_native;
  494. map->format.parse_val = regmap_parse_16_native;
  495. break;
  496. default:
  497. goto err_map;
  498. }
  499. break;
  500. case 24:
  501. if (val_endian != REGMAP_ENDIAN_BIG)
  502. goto err_map;
  503. map->format.format_val = regmap_format_24;
  504. map->format.parse_val = regmap_parse_24;
  505. break;
  506. case 32:
  507. switch (val_endian) {
  508. case REGMAP_ENDIAN_BIG:
  509. map->format.format_val = regmap_format_32_be;
  510. map->format.parse_val = regmap_parse_32_be;
  511. map->format.parse_inplace = regmap_parse_32_be_inplace;
  512. break;
  513. case REGMAP_ENDIAN_NATIVE:
  514. map->format.format_val = regmap_format_32_native;
  515. map->format.parse_val = regmap_parse_32_native;
  516. break;
  517. default:
  518. goto err_map;
  519. }
  520. break;
  521. }
  522. if (map->format.format_write) {
  523. if ((reg_endian != REGMAP_ENDIAN_BIG) ||
  524. (val_endian != REGMAP_ENDIAN_BIG))
  525. goto err_map;
  526. map->use_single_rw = true;
  527. }
  528. if (!map->format.format_write &&
  529. !(map->format.format_reg && map->format.format_val))
  530. goto err_map;
  531. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  532. if (map->work_buf == NULL) {
  533. ret = -ENOMEM;
  534. goto err_map;
  535. }
  536. if (map->format.format_write) {
  537. map->defer_caching = false;
  538. map->reg_write = _regmap_bus_formatted_write;
  539. } else if (map->format.format_val) {
  540. map->defer_caching = true;
  541. map->reg_write = _regmap_bus_raw_write;
  542. }
  543. skip_format_initialization:
  544. map->range_tree = RB_ROOT;
  545. for (i = 0; i < config->num_ranges; i++) {
  546. const struct regmap_range_cfg *range_cfg = &config->ranges[i];
  547. struct regmap_range_node *new;
  548. /* Sanity check */
  549. if (range_cfg->range_max < range_cfg->range_min) {
  550. dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
  551. range_cfg->range_max, range_cfg->range_min);
  552. goto err_range;
  553. }
  554. if (range_cfg->range_max > map->max_register) {
  555. dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
  556. range_cfg->range_max, map->max_register);
  557. goto err_range;
  558. }
  559. if (range_cfg->selector_reg > map->max_register) {
  560. dev_err(map->dev,
  561. "Invalid range %d: selector out of map\n", i);
  562. goto err_range;
  563. }
  564. if (range_cfg->window_len == 0) {
  565. dev_err(map->dev, "Invalid range %d: window_len 0\n",
  566. i);
  567. goto err_range;
  568. }
  569. /* Make sure, that this register range has no selector
  570. or data window within its boundary */
  571. for (j = 0; j < config->num_ranges; j++) {
  572. unsigned sel_reg = config->ranges[j].selector_reg;
  573. unsigned win_min = config->ranges[j].window_start;
  574. unsigned win_max = win_min +
  575. config->ranges[j].window_len - 1;
  576. /* Allow data window inside its own virtual range */
  577. if (j == i)
  578. continue;
  579. if (range_cfg->range_min <= sel_reg &&
  580. sel_reg <= range_cfg->range_max) {
  581. dev_err(map->dev,
  582. "Range %d: selector for %d in window\n",
  583. i, j);
  584. goto err_range;
  585. }
  586. if (!(win_max < range_cfg->range_min ||
  587. win_min > range_cfg->range_max)) {
  588. dev_err(map->dev,
  589. "Range %d: window for %d in window\n",
  590. i, j);
  591. goto err_range;
  592. }
  593. }
  594. new = kzalloc(sizeof(*new), GFP_KERNEL);
  595. if (new == NULL) {
  596. ret = -ENOMEM;
  597. goto err_range;
  598. }
  599. new->map = map;
  600. new->name = range_cfg->name;
  601. new->range_min = range_cfg->range_min;
  602. new->range_max = range_cfg->range_max;
  603. new->selector_reg = range_cfg->selector_reg;
  604. new->selector_mask = range_cfg->selector_mask;
  605. new->selector_shift = range_cfg->selector_shift;
  606. new->window_start = range_cfg->window_start;
  607. new->window_len = range_cfg->window_len;
  608. if (_regmap_range_add(map, new) == false) {
  609. dev_err(map->dev, "Failed to add range %d\n", i);
  610. kfree(new);
  611. goto err_range;
  612. }
  613. if (map->selector_work_buf == NULL) {
  614. map->selector_work_buf =
  615. kzalloc(map->format.buf_size, GFP_KERNEL);
  616. if (map->selector_work_buf == NULL) {
  617. ret = -ENOMEM;
  618. goto err_range;
  619. }
  620. }
  621. }
  622. regmap_debugfs_init(map, config->name);
  623. ret = regcache_init(map, config);
  624. if (ret != 0)
  625. goto err_range;
  626. /* Add a devres resource for dev_get_regmap() */
  627. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  628. if (!m) {
  629. ret = -ENOMEM;
  630. goto err_debugfs;
  631. }
  632. *m = map;
  633. devres_add(dev, m);
  634. return map;
  635. err_debugfs:
  636. regmap_debugfs_exit(map);
  637. regcache_exit(map);
  638. err_range:
  639. regmap_range_exit(map);
  640. kfree(map->work_buf);
  641. err_map:
  642. kfree(map);
  643. err:
  644. return ERR_PTR(ret);
  645. }
  646. EXPORT_SYMBOL_GPL(regmap_init);
  647. static void devm_regmap_release(struct device *dev, void *res)
  648. {
  649. regmap_exit(*(struct regmap **)res);
  650. }
  651. /**
  652. * devm_regmap_init(): Initialise managed register map
  653. *
  654. * @dev: Device that will be interacted with
  655. * @bus: Bus-specific callbacks to use with device
  656. * @bus_context: Data passed to bus-specific callbacks
  657. * @config: Configuration for register map
  658. *
  659. * The return value will be an ERR_PTR() on error or a valid pointer
  660. * to a struct regmap. This function should generally not be called
  661. * directly, it should be called by bus-specific init functions. The
  662. * map will be automatically freed by the device management code.
  663. */
  664. struct regmap *devm_regmap_init(struct device *dev,
  665. const struct regmap_bus *bus,
  666. void *bus_context,
  667. const struct regmap_config *config)
  668. {
  669. struct regmap **ptr, *regmap;
  670. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  671. if (!ptr)
  672. return ERR_PTR(-ENOMEM);
  673. regmap = regmap_init(dev, bus, bus_context, config);
  674. if (!IS_ERR(regmap)) {
  675. *ptr = regmap;
  676. devres_add(dev, ptr);
  677. } else {
  678. devres_free(ptr);
  679. }
  680. return regmap;
  681. }
  682. EXPORT_SYMBOL_GPL(devm_regmap_init);
  683. static void regmap_field_init(struct regmap_field *rm_field,
  684. struct regmap *regmap, struct reg_field reg_field)
  685. {
  686. int field_bits = reg_field.msb - reg_field.lsb + 1;
  687. rm_field->regmap = regmap;
  688. rm_field->reg = reg_field.reg;
  689. rm_field->shift = reg_field.lsb;
  690. rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
  691. }
  692. /**
  693. * devm_regmap_field_alloc(): Allocate and initialise a register field
  694. * in a register map.
  695. *
  696. * @dev: Device that will be interacted with
  697. * @regmap: regmap bank in which this register field is located.
  698. * @reg_field: Register field with in the bank.
  699. *
  700. * The return value will be an ERR_PTR() on error or a valid pointer
  701. * to a struct regmap_field. The regmap_field will be automatically freed
  702. * by the device management code.
  703. */
  704. struct regmap_field *devm_regmap_field_alloc(struct device *dev,
  705. struct regmap *regmap, struct reg_field reg_field)
  706. {
  707. struct regmap_field *rm_field = devm_kzalloc(dev,
  708. sizeof(*rm_field), GFP_KERNEL);
  709. if (!rm_field)
  710. return ERR_PTR(-ENOMEM);
  711. regmap_field_init(rm_field, regmap, reg_field);
  712. return rm_field;
  713. }
  714. EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
  715. /**
  716. * devm_regmap_field_free(): Free register field allocated using
  717. * devm_regmap_field_alloc. Usally drivers need not call this function,
  718. * as the memory allocated via devm will be freed as per device-driver
  719. * life-cyle.
  720. *
  721. * @dev: Device that will be interacted with
  722. * @field: regmap field which should be freed.
  723. */
  724. void devm_regmap_field_free(struct device *dev,
  725. struct regmap_field *field)
  726. {
  727. devm_kfree(dev, field);
  728. }
  729. EXPORT_SYMBOL_GPL(devm_regmap_field_free);
  730. /**
  731. * regmap_field_alloc(): Allocate and initialise a register field
  732. * in a register map.
  733. *
  734. * @regmap: regmap bank in which this register field is located.
  735. * @reg_field: Register field with in the bank.
  736. *
  737. * The return value will be an ERR_PTR() on error or a valid pointer
  738. * to a struct regmap_field. The regmap_field should be freed by the
  739. * user once its finished working with it using regmap_field_free().
  740. */
  741. struct regmap_field *regmap_field_alloc(struct regmap *regmap,
  742. struct reg_field reg_field)
  743. {
  744. struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
  745. if (!rm_field)
  746. return ERR_PTR(-ENOMEM);
  747. regmap_field_init(rm_field, regmap, reg_field);
  748. return rm_field;
  749. }
  750. EXPORT_SYMBOL_GPL(regmap_field_alloc);
  751. /**
  752. * regmap_field_free(): Free register field allocated using regmap_field_alloc
  753. *
  754. * @field: regmap field which should be freed.
  755. */
  756. void regmap_field_free(struct regmap_field *field)
  757. {
  758. kfree(field);
  759. }
  760. EXPORT_SYMBOL_GPL(regmap_field_free);
  761. /**
  762. * regmap_reinit_cache(): Reinitialise the current register cache
  763. *
  764. * @map: Register map to operate on.
  765. * @config: New configuration. Only the cache data will be used.
  766. *
  767. * Discard any existing register cache for the map and initialize a
  768. * new cache. This can be used to restore the cache to defaults or to
  769. * update the cache configuration to reflect runtime discovery of the
  770. * hardware.
  771. *
  772. * No explicit locking is done here, the user needs to ensure that
  773. * this function will not race with other calls to regmap.
  774. */
  775. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  776. {
  777. regcache_exit(map);
  778. regmap_debugfs_exit(map);
  779. map->max_register = config->max_register;
  780. map->writeable_reg = config->writeable_reg;
  781. map->readable_reg = config->readable_reg;
  782. map->volatile_reg = config->volatile_reg;
  783. map->precious_reg = config->precious_reg;
  784. map->cache_type = config->cache_type;
  785. regmap_debugfs_init(map, config->name);
  786. map->cache_bypass = false;
  787. map->cache_only = false;
  788. return regcache_init(map, config);
  789. }
  790. EXPORT_SYMBOL_GPL(regmap_reinit_cache);
  791. /**
  792. * regmap_exit(): Free a previously allocated register map
  793. */
  794. void regmap_exit(struct regmap *map)
  795. {
  796. regcache_exit(map);
  797. regmap_debugfs_exit(map);
  798. regmap_range_exit(map);
  799. if (map->bus && map->bus->free_context)
  800. map->bus->free_context(map->bus_context);
  801. kfree(map->work_buf);
  802. kfree(map);
  803. }
  804. EXPORT_SYMBOL_GPL(regmap_exit);
  805. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  806. {
  807. struct regmap **r = res;
  808. if (!r || !*r) {
  809. WARN_ON(!r || !*r);
  810. return 0;
  811. }
  812. /* If the user didn't specify a name match any */
  813. if (data)
  814. return (*r)->name == data;
  815. else
  816. return 1;
  817. }
  818. /**
  819. * dev_get_regmap(): Obtain the regmap (if any) for a device
  820. *
  821. * @dev: Device to retrieve the map for
  822. * @name: Optional name for the register map, usually NULL.
  823. *
  824. * Returns the regmap for the device if one is present, or NULL. If
  825. * name is specified then it must match the name specified when
  826. * registering the device, if it is NULL then the first regmap found
  827. * will be used. Devices with multiple register maps are very rare,
  828. * generic code should normally not need to specify a name.
  829. */
  830. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  831. {
  832. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  833. dev_get_regmap_match, (void *)name);
  834. if (!r)
  835. return NULL;
  836. return *r;
  837. }
  838. EXPORT_SYMBOL_GPL(dev_get_regmap);
  839. static int _regmap_select_page(struct regmap *map, unsigned int *reg,
  840. struct regmap_range_node *range,
  841. unsigned int val_num)
  842. {
  843. void *orig_work_buf;
  844. unsigned int win_offset;
  845. unsigned int win_page;
  846. bool page_chg;
  847. int ret;
  848. win_offset = (*reg - range->range_min) % range->window_len;
  849. win_page = (*reg - range->range_min) / range->window_len;
  850. if (val_num > 1) {
  851. /* Bulk write shouldn't cross range boundary */
  852. if (*reg + val_num - 1 > range->range_max)
  853. return -EINVAL;
  854. /* ... or single page boundary */
  855. if (val_num > range->window_len - win_offset)
  856. return -EINVAL;
  857. }
  858. /* It is possible to have selector register inside data window.
  859. In that case, selector register is located on every page and
  860. it needs no page switching, when accessed alone. */
  861. if (val_num > 1 ||
  862. range->window_start + win_offset != range->selector_reg) {
  863. /* Use separate work_buf during page switching */
  864. orig_work_buf = map->work_buf;
  865. map->work_buf = map->selector_work_buf;
  866. ret = _regmap_update_bits(map, range->selector_reg,
  867. range->selector_mask,
  868. win_page << range->selector_shift,
  869. &page_chg);
  870. map->work_buf = orig_work_buf;
  871. if (ret != 0)
  872. return ret;
  873. }
  874. *reg = range->window_start + win_offset;
  875. return 0;
  876. }
  877. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  878. const void *val, size_t val_len, bool async)
  879. {
  880. struct regmap_range_node *range;
  881. unsigned long flags;
  882. u8 *u8 = map->work_buf;
  883. void *work_val = map->work_buf + map->format.reg_bytes +
  884. map->format.pad_bytes;
  885. void *buf;
  886. int ret = -ENOTSUPP;
  887. size_t len;
  888. int i;
  889. WARN_ON(!map->bus);
  890. /* Check for unwritable registers before we start */
  891. if (map->writeable_reg)
  892. for (i = 0; i < val_len / map->format.val_bytes; i++)
  893. if (!map->writeable_reg(map->dev,
  894. reg + (i * map->reg_stride)))
  895. return -EINVAL;
  896. if (!map->cache_bypass && map->format.parse_val) {
  897. unsigned int ival;
  898. int val_bytes = map->format.val_bytes;
  899. for (i = 0; i < val_len / val_bytes; i++) {
  900. ival = map->format.parse_val(val + (i * val_bytes));
  901. ret = regcache_write(map, reg + (i * map->reg_stride),
  902. ival);
  903. if (ret) {
  904. dev_err(map->dev,
  905. "Error in caching of register: %x ret: %d\n",
  906. reg + i, ret);
  907. return ret;
  908. }
  909. }
  910. if (map->cache_only) {
  911. map->cache_dirty = true;
  912. return 0;
  913. }
  914. }
  915. range = _regmap_range_lookup(map, reg);
  916. if (range) {
  917. int val_num = val_len / map->format.val_bytes;
  918. int win_offset = (reg - range->range_min) % range->window_len;
  919. int win_residue = range->window_len - win_offset;
  920. /* If the write goes beyond the end of the window split it */
  921. while (val_num > win_residue) {
  922. dev_dbg(map->dev, "Writing window %d/%zu\n",
  923. win_residue, val_len / map->format.val_bytes);
  924. ret = _regmap_raw_write(map, reg, val, win_residue *
  925. map->format.val_bytes, async);
  926. if (ret != 0)
  927. return ret;
  928. reg += win_residue;
  929. val_num -= win_residue;
  930. val += win_residue * map->format.val_bytes;
  931. val_len -= win_residue * map->format.val_bytes;
  932. win_offset = (reg - range->range_min) %
  933. range->window_len;
  934. win_residue = range->window_len - win_offset;
  935. }
  936. ret = _regmap_select_page(map, &reg, range, val_num);
  937. if (ret != 0)
  938. return ret;
  939. }
  940. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  941. u8[0] |= map->write_flag_mask;
  942. if (async && map->bus->async_write) {
  943. struct regmap_async *async = map->bus->async_alloc();
  944. if (!async)
  945. return -ENOMEM;
  946. trace_regmap_async_write_start(map->dev, reg, val_len);
  947. async->work_buf = kzalloc(map->format.buf_size,
  948. GFP_KERNEL | GFP_DMA);
  949. if (!async->work_buf) {
  950. kfree(async);
  951. return -ENOMEM;
  952. }
  953. INIT_WORK(&async->cleanup, async_cleanup);
  954. async->map = map;
  955. /* If the caller supplied the value we can use it safely. */
  956. memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
  957. map->format.reg_bytes + map->format.val_bytes);
  958. if (val == work_val)
  959. val = async->work_buf + map->format.pad_bytes +
  960. map->format.reg_bytes;
  961. spin_lock_irqsave(&map->async_lock, flags);
  962. list_add_tail(&async->list, &map->async_list);
  963. spin_unlock_irqrestore(&map->async_lock, flags);
  964. ret = map->bus->async_write(map->bus_context, async->work_buf,
  965. map->format.reg_bytes +
  966. map->format.pad_bytes,
  967. val, val_len, async);
  968. if (ret != 0) {
  969. dev_err(map->dev, "Failed to schedule write: %d\n",
  970. ret);
  971. spin_lock_irqsave(&map->async_lock, flags);
  972. list_del(&async->list);
  973. spin_unlock_irqrestore(&map->async_lock, flags);
  974. kfree(async->work_buf);
  975. kfree(async);
  976. }
  977. return ret;
  978. }
  979. trace_regmap_hw_write_start(map->dev, reg,
  980. val_len / map->format.val_bytes);
  981. /* If we're doing a single register write we can probably just
  982. * send the work_buf directly, otherwise try to do a gather
  983. * write.
  984. */
  985. if (val == work_val)
  986. ret = map->bus->write(map->bus_context, map->work_buf,
  987. map->format.reg_bytes +
  988. map->format.pad_bytes +
  989. val_len);
  990. else if (map->bus->gather_write)
  991. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  992. map->format.reg_bytes +
  993. map->format.pad_bytes,
  994. val, val_len);
  995. /* If that didn't work fall back on linearising by hand. */
  996. if (ret == -ENOTSUPP) {
  997. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  998. buf = kzalloc(len, GFP_KERNEL);
  999. if (!buf)
  1000. return -ENOMEM;
  1001. memcpy(buf, map->work_buf, map->format.reg_bytes);
  1002. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  1003. val, val_len);
  1004. ret = map->bus->write(map->bus_context, buf, len);
  1005. kfree(buf);
  1006. }
  1007. trace_regmap_hw_write_done(map->dev, reg,
  1008. val_len / map->format.val_bytes);
  1009. return ret;
  1010. }
  1011. /**
  1012. * regmap_can_raw_write - Test if regmap_raw_write() is supported
  1013. *
  1014. * @map: Map to check.
  1015. */
  1016. bool regmap_can_raw_write(struct regmap *map)
  1017. {
  1018. return map->bus && map->format.format_val && map->format.format_reg;
  1019. }
  1020. EXPORT_SYMBOL_GPL(regmap_can_raw_write);
  1021. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  1022. unsigned int val)
  1023. {
  1024. int ret;
  1025. struct regmap_range_node *range;
  1026. struct regmap *map = context;
  1027. WARN_ON(!map->bus || !map->format.format_write);
  1028. range = _regmap_range_lookup(map, reg);
  1029. if (range) {
  1030. ret = _regmap_select_page(map, &reg, range, 1);
  1031. if (ret != 0)
  1032. return ret;
  1033. }
  1034. map->format.format_write(map, reg, val);
  1035. trace_regmap_hw_write_start(map->dev, reg, 1);
  1036. ret = map->bus->write(map->bus_context, map->work_buf,
  1037. map->format.buf_size);
  1038. trace_regmap_hw_write_done(map->dev, reg, 1);
  1039. return ret;
  1040. }
  1041. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  1042. unsigned int val)
  1043. {
  1044. struct regmap *map = context;
  1045. WARN_ON(!map->bus || !map->format.format_val);
  1046. map->format.format_val(map->work_buf + map->format.reg_bytes
  1047. + map->format.pad_bytes, val, 0);
  1048. return _regmap_raw_write(map, reg,
  1049. map->work_buf +
  1050. map->format.reg_bytes +
  1051. map->format.pad_bytes,
  1052. map->format.val_bytes, false);
  1053. }
  1054. static inline void *_regmap_map_get_context(struct regmap *map)
  1055. {
  1056. return (map->bus) ? map : map->bus_context;
  1057. }
  1058. int _regmap_write(struct regmap *map, unsigned int reg,
  1059. unsigned int val)
  1060. {
  1061. int ret;
  1062. void *context = _regmap_map_get_context(map);
  1063. if (!regmap_writeable(map, reg))
  1064. return -EIO;
  1065. if (!map->cache_bypass && !map->defer_caching) {
  1066. ret = regcache_write(map, reg, val);
  1067. if (ret != 0)
  1068. return ret;
  1069. if (map->cache_only) {
  1070. map->cache_dirty = true;
  1071. return 0;
  1072. }
  1073. }
  1074. #ifdef LOG_DEVICE
  1075. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1076. dev_info(map->dev, "%x <= %x\n", reg, val);
  1077. #endif
  1078. trace_regmap_reg_write(map->dev, reg, val);
  1079. return map->reg_write(context, reg, val);
  1080. }
  1081. /**
  1082. * regmap_write(): Write a value to a single register
  1083. *
  1084. * @map: Register map to write to
  1085. * @reg: Register to write to
  1086. * @val: Value to be written
  1087. *
  1088. * A value of zero will be returned on success, a negative errno will
  1089. * be returned in error cases.
  1090. */
  1091. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  1092. {
  1093. int ret;
  1094. if (reg % map->reg_stride)
  1095. return -EINVAL;
  1096. map->lock(map->lock_arg);
  1097. ret = _regmap_write(map, reg, val);
  1098. map->unlock(map->lock_arg);
  1099. return ret;
  1100. }
  1101. EXPORT_SYMBOL_GPL(regmap_write);
  1102. /**
  1103. * regmap_raw_write(): Write raw values to one or more registers
  1104. *
  1105. * @map: Register map to write to
  1106. * @reg: Initial register to write to
  1107. * @val: Block of data to be written, laid out for direct transmission to the
  1108. * device
  1109. * @val_len: Length of data pointed to by val.
  1110. *
  1111. * This function is intended to be used for things like firmware
  1112. * download where a large block of data needs to be transferred to the
  1113. * device. No formatting will be done on the data provided.
  1114. *
  1115. * A value of zero will be returned on success, a negative errno will
  1116. * be returned in error cases.
  1117. */
  1118. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1119. const void *val, size_t val_len)
  1120. {
  1121. int ret;
  1122. if (!regmap_can_raw_write(map))
  1123. return -EINVAL;
  1124. if (val_len % map->format.val_bytes)
  1125. return -EINVAL;
  1126. map->lock(map->lock_arg);
  1127. ret = _regmap_raw_write(map, reg, val, val_len, false);
  1128. map->unlock(map->lock_arg);
  1129. return ret;
  1130. }
  1131. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1132. /**
  1133. * regmap_field_write(): Write a value to a single register field
  1134. *
  1135. * @field: Register field to write to
  1136. * @val: Value to be written
  1137. *
  1138. * A value of zero will be returned on success, a negative errno will
  1139. * be returned in error cases.
  1140. */
  1141. int regmap_field_write(struct regmap_field *field, unsigned int val)
  1142. {
  1143. return regmap_update_bits(field->regmap, field->reg,
  1144. field->mask, val << field->shift);
  1145. }
  1146. EXPORT_SYMBOL_GPL(regmap_field_write);
  1147. /*
  1148. * regmap_bulk_write(): Write multiple registers to the device
  1149. *
  1150. * @map: Register map to write to
  1151. * @reg: First register to be write from
  1152. * @val: Block of data to be written, in native register size for device
  1153. * @val_count: Number of registers to write
  1154. *
  1155. * This function is intended to be used for writing a large block of
  1156. * data to the device either in single transfer or multiple transfer.
  1157. *
  1158. * A value of zero will be returned on success, a negative errno will
  1159. * be returned in error cases.
  1160. */
  1161. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1162. size_t val_count)
  1163. {
  1164. int ret = 0, i;
  1165. size_t val_bytes = map->format.val_bytes;
  1166. void *wval;
  1167. if (!map->bus)
  1168. return -EINVAL;
  1169. if (!map->format.parse_inplace)
  1170. return -EINVAL;
  1171. if (reg % map->reg_stride)
  1172. return -EINVAL;
  1173. map->lock(map->lock_arg);
  1174. /* No formatting is require if val_byte is 1 */
  1175. if (val_bytes == 1) {
  1176. wval = (void *)val;
  1177. } else {
  1178. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  1179. if (!wval) {
  1180. ret = -ENOMEM;
  1181. dev_err(map->dev, "Error in memory allocation\n");
  1182. goto out;
  1183. }
  1184. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1185. map->format.parse_inplace(wval + i);
  1186. }
  1187. /*
  1188. * Some devices does not support bulk write, for
  1189. * them we have a series of single write operations.
  1190. */
  1191. if (map->use_single_rw) {
  1192. for (i = 0; i < val_count; i++) {
  1193. ret = regmap_raw_write(map,
  1194. reg + (i * map->reg_stride),
  1195. val + (i * val_bytes),
  1196. val_bytes);
  1197. if (ret != 0)
  1198. return ret;
  1199. }
  1200. } else {
  1201. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count,
  1202. false);
  1203. }
  1204. if (val_bytes != 1)
  1205. kfree(wval);
  1206. out:
  1207. map->unlock(map->lock_arg);
  1208. return ret;
  1209. }
  1210. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1211. /**
  1212. * regmap_raw_write_async(): Write raw values to one or more registers
  1213. * asynchronously
  1214. *
  1215. * @map: Register map to write to
  1216. * @reg: Initial register to write to
  1217. * @val: Block of data to be written, laid out for direct transmission to the
  1218. * device. Must be valid until regmap_async_complete() is called.
  1219. * @val_len: Length of data pointed to by val.
  1220. *
  1221. * This function is intended to be used for things like firmware
  1222. * download where a large block of data needs to be transferred to the
  1223. * device. No formatting will be done on the data provided.
  1224. *
  1225. * If supported by the underlying bus the write will be scheduled
  1226. * asynchronously, helping maximise I/O speed on higher speed buses
  1227. * like SPI. regmap_async_complete() can be called to ensure that all
  1228. * asynchrnous writes have been completed.
  1229. *
  1230. * A value of zero will be returned on success, a negative errno will
  1231. * be returned in error cases.
  1232. */
  1233. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  1234. const void *val, size_t val_len)
  1235. {
  1236. int ret;
  1237. if (val_len % map->format.val_bytes)
  1238. return -EINVAL;
  1239. if (reg % map->reg_stride)
  1240. return -EINVAL;
  1241. map->lock(map->lock_arg);
  1242. ret = _regmap_raw_write(map, reg, val, val_len, true);
  1243. map->unlock(map->lock_arg);
  1244. return ret;
  1245. }
  1246. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  1247. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1248. unsigned int val_len)
  1249. {
  1250. struct regmap_range_node *range;
  1251. u8 *u8 = map->work_buf;
  1252. int ret;
  1253. WARN_ON(!map->bus);
  1254. range = _regmap_range_lookup(map, reg);
  1255. if (range) {
  1256. ret = _regmap_select_page(map, &reg, range,
  1257. val_len / map->format.val_bytes);
  1258. if (ret != 0)
  1259. return ret;
  1260. }
  1261. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1262. /*
  1263. * Some buses or devices flag reads by setting the high bits in the
  1264. * register addresss; since it's always the high bits for all
  1265. * current formats we can do this here rather than in
  1266. * formatting. This may break if we get interesting formats.
  1267. */
  1268. u8[0] |= map->read_flag_mask;
  1269. trace_regmap_hw_read_start(map->dev, reg,
  1270. val_len / map->format.val_bytes);
  1271. ret = map->bus->read(map->bus_context, map->work_buf,
  1272. map->format.reg_bytes + map->format.pad_bytes,
  1273. val, val_len);
  1274. trace_regmap_hw_read_done(map->dev, reg,
  1275. val_len / map->format.val_bytes);
  1276. return ret;
  1277. }
  1278. static int _regmap_bus_read(void *context, unsigned int reg,
  1279. unsigned int *val)
  1280. {
  1281. int ret;
  1282. struct regmap *map = context;
  1283. if (!map->format.parse_val)
  1284. return -EINVAL;
  1285. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  1286. if (ret == 0)
  1287. *val = map->format.parse_val(map->work_buf);
  1288. return ret;
  1289. }
  1290. static int _regmap_read(struct regmap *map, unsigned int reg,
  1291. unsigned int *val)
  1292. {
  1293. int ret;
  1294. void *context = _regmap_map_get_context(map);
  1295. WARN_ON(!map->reg_read);
  1296. if (!map->cache_bypass) {
  1297. ret = regcache_read(map, reg, val);
  1298. if (ret == 0)
  1299. return 0;
  1300. }
  1301. if (map->cache_only)
  1302. return -EBUSY;
  1303. ret = map->reg_read(context, reg, val);
  1304. if (ret == 0) {
  1305. #ifdef LOG_DEVICE
  1306. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1307. dev_info(map->dev, "%x => %x\n", reg, *val);
  1308. #endif
  1309. trace_regmap_reg_read(map->dev, reg, *val);
  1310. if (!map->cache_bypass)
  1311. regcache_write(map, reg, *val);
  1312. }
  1313. return ret;
  1314. }
  1315. /**
  1316. * regmap_read(): Read a value from a single register
  1317. *
  1318. * @map: Register map to write to
  1319. * @reg: Register to be read from
  1320. * @val: Pointer to store read value
  1321. *
  1322. * A value of zero will be returned on success, a negative errno will
  1323. * be returned in error cases.
  1324. */
  1325. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  1326. {
  1327. int ret;
  1328. if (reg % map->reg_stride)
  1329. return -EINVAL;
  1330. map->lock(map->lock_arg);
  1331. ret = _regmap_read(map, reg, val);
  1332. map->unlock(map->lock_arg);
  1333. return ret;
  1334. }
  1335. EXPORT_SYMBOL_GPL(regmap_read);
  1336. /**
  1337. * regmap_raw_read(): Read raw data from the device
  1338. *
  1339. * @map: Register map to write to
  1340. * @reg: First register to be read from
  1341. * @val: Pointer to store read value
  1342. * @val_len: Size of data to read
  1343. *
  1344. * A value of zero will be returned on success, a negative errno will
  1345. * be returned in error cases.
  1346. */
  1347. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1348. size_t val_len)
  1349. {
  1350. size_t val_bytes = map->format.val_bytes;
  1351. size_t val_count = val_len / val_bytes;
  1352. unsigned int v;
  1353. int ret, i;
  1354. if (!map->bus)
  1355. return -EINVAL;
  1356. if (val_len % map->format.val_bytes)
  1357. return -EINVAL;
  1358. if (reg % map->reg_stride)
  1359. return -EINVAL;
  1360. map->lock(map->lock_arg);
  1361. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  1362. map->cache_type == REGCACHE_NONE) {
  1363. /* Physical block read if there's no cache involved */
  1364. ret = _regmap_raw_read(map, reg, val, val_len);
  1365. } else {
  1366. /* Otherwise go word by word for the cache; should be low
  1367. * cost as we expect to hit the cache.
  1368. */
  1369. for (i = 0; i < val_count; i++) {
  1370. ret = _regmap_read(map, reg + (i * map->reg_stride),
  1371. &v);
  1372. if (ret != 0)
  1373. goto out;
  1374. map->format.format_val(val + (i * val_bytes), v, 0);
  1375. }
  1376. }
  1377. out:
  1378. map->unlock(map->lock_arg);
  1379. return ret;
  1380. }
  1381. EXPORT_SYMBOL_GPL(regmap_raw_read);
  1382. /**
  1383. * regmap_field_read(): Read a value to a single register field
  1384. *
  1385. * @field: Register field to read from
  1386. * @val: Pointer to store read value
  1387. *
  1388. * A value of zero will be returned on success, a negative errno will
  1389. * be returned in error cases.
  1390. */
  1391. int regmap_field_read(struct regmap_field *field, unsigned int *val)
  1392. {
  1393. int ret;
  1394. unsigned int reg_val;
  1395. ret = regmap_read(field->regmap, field->reg, &reg_val);
  1396. if (ret != 0)
  1397. return ret;
  1398. reg_val &= field->mask;
  1399. reg_val >>= field->shift;
  1400. *val = reg_val;
  1401. return ret;
  1402. }
  1403. EXPORT_SYMBOL_GPL(regmap_field_read);
  1404. /**
  1405. * regmap_bulk_read(): Read multiple registers from the device
  1406. *
  1407. * @map: Register map to write to
  1408. * @reg: First register to be read from
  1409. * @val: Pointer to store read value, in native register size for device
  1410. * @val_count: Number of registers to read
  1411. *
  1412. * A value of zero will be returned on success, a negative errno will
  1413. * be returned in error cases.
  1414. */
  1415. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  1416. size_t val_count)
  1417. {
  1418. int ret, i;
  1419. size_t val_bytes = map->format.val_bytes;
  1420. bool vol = regmap_volatile_range(map, reg, val_count);
  1421. if (!map->bus)
  1422. return -EINVAL;
  1423. if (!map->format.parse_inplace)
  1424. return -EINVAL;
  1425. if (reg % map->reg_stride)
  1426. return -EINVAL;
  1427. if (vol || map->cache_type == REGCACHE_NONE) {
  1428. /*
  1429. * Some devices does not support bulk read, for
  1430. * them we have a series of single read operations.
  1431. */
  1432. if (map->use_single_rw) {
  1433. for (i = 0; i < val_count; i++) {
  1434. ret = regmap_raw_read(map,
  1435. reg + (i * map->reg_stride),
  1436. val + (i * val_bytes),
  1437. val_bytes);
  1438. if (ret != 0)
  1439. return ret;
  1440. }
  1441. } else {
  1442. ret = regmap_raw_read(map, reg, val,
  1443. val_bytes * val_count);
  1444. if (ret != 0)
  1445. return ret;
  1446. }
  1447. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1448. map->format.parse_inplace(val + i);
  1449. } else {
  1450. for (i = 0; i < val_count; i++) {
  1451. unsigned int ival;
  1452. ret = regmap_read(map, reg + (i * map->reg_stride),
  1453. &ival);
  1454. if (ret != 0)
  1455. return ret;
  1456. memcpy(val + (i * val_bytes), &ival, val_bytes);
  1457. }
  1458. }
  1459. return 0;
  1460. }
  1461. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  1462. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  1463. unsigned int mask, unsigned int val,
  1464. bool *change)
  1465. {
  1466. int ret;
  1467. unsigned int tmp, orig;
  1468. ret = _regmap_read(map, reg, &orig);
  1469. if (ret != 0)
  1470. return ret;
  1471. tmp = orig & ~mask;
  1472. tmp |= val & mask;
  1473. if (tmp != orig) {
  1474. ret = _regmap_write(map, reg, tmp);
  1475. *change = true;
  1476. } else {
  1477. *change = false;
  1478. }
  1479. return ret;
  1480. }
  1481. /**
  1482. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  1483. *
  1484. * @map: Register map to update
  1485. * @reg: Register to update
  1486. * @mask: Bitmask to change
  1487. * @val: New value for bitmask
  1488. *
  1489. * Returns zero for success, a negative number on error.
  1490. */
  1491. int regmap_update_bits(struct regmap *map, unsigned int reg,
  1492. unsigned int mask, unsigned int val)
  1493. {
  1494. bool change;
  1495. int ret;
  1496. map->lock(map->lock_arg);
  1497. ret = _regmap_update_bits(map, reg, mask, val, &change);
  1498. map->unlock(map->lock_arg);
  1499. return ret;
  1500. }
  1501. EXPORT_SYMBOL_GPL(regmap_update_bits);
  1502. /**
  1503. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  1504. * register map and report if updated
  1505. *
  1506. * @map: Register map to update
  1507. * @reg: Register to update
  1508. * @mask: Bitmask to change
  1509. * @val: New value for bitmask
  1510. * @change: Boolean indicating if a write was done
  1511. *
  1512. * Returns zero for success, a negative number on error.
  1513. */
  1514. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  1515. unsigned int mask, unsigned int val,
  1516. bool *change)
  1517. {
  1518. int ret;
  1519. map->lock(map->lock_arg);
  1520. ret = _regmap_update_bits(map, reg, mask, val, change);
  1521. map->unlock(map->lock_arg);
  1522. return ret;
  1523. }
  1524. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  1525. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  1526. {
  1527. struct regmap *map = async->map;
  1528. bool wake;
  1529. trace_regmap_async_io_complete(map->dev);
  1530. spin_lock(&map->async_lock);
  1531. list_del(&async->list);
  1532. wake = list_empty(&map->async_list);
  1533. if (ret != 0)
  1534. map->async_ret = ret;
  1535. spin_unlock(&map->async_lock);
  1536. schedule_work(&async->cleanup);
  1537. if (wake)
  1538. wake_up(&map->async_waitq);
  1539. }
  1540. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  1541. static int regmap_async_is_done(struct regmap *map)
  1542. {
  1543. unsigned long flags;
  1544. int ret;
  1545. spin_lock_irqsave(&map->async_lock, flags);
  1546. ret = list_empty(&map->async_list);
  1547. spin_unlock_irqrestore(&map->async_lock, flags);
  1548. return ret;
  1549. }
  1550. /**
  1551. * regmap_async_complete: Ensure all asynchronous I/O has completed.
  1552. *
  1553. * @map: Map to operate on.
  1554. *
  1555. * Blocks until any pending asynchronous I/O has completed. Returns
  1556. * an error code for any failed I/O operations.
  1557. */
  1558. int regmap_async_complete(struct regmap *map)
  1559. {
  1560. unsigned long flags;
  1561. int ret;
  1562. /* Nothing to do with no async support */
  1563. if (!map->bus || !map->bus->async_write)
  1564. return 0;
  1565. trace_regmap_async_complete_start(map->dev);
  1566. wait_event(map->async_waitq, regmap_async_is_done(map));
  1567. spin_lock_irqsave(&map->async_lock, flags);
  1568. ret = map->async_ret;
  1569. map->async_ret = 0;
  1570. spin_unlock_irqrestore(&map->async_lock, flags);
  1571. trace_regmap_async_complete_done(map->dev);
  1572. return ret;
  1573. }
  1574. EXPORT_SYMBOL_GPL(regmap_async_complete);
  1575. /**
  1576. * regmap_register_patch: Register and apply register updates to be applied
  1577. * on device initialistion
  1578. *
  1579. * @map: Register map to apply updates to.
  1580. * @regs: Values to update.
  1581. * @num_regs: Number of entries in regs.
  1582. *
  1583. * Register a set of register updates to be applied to the device
  1584. * whenever the device registers are synchronised with the cache and
  1585. * apply them immediately. Typically this is used to apply
  1586. * corrections to be applied to the device defaults on startup, such
  1587. * as the updates some vendors provide to undocumented registers.
  1588. */
  1589. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  1590. int num_regs)
  1591. {
  1592. struct reg_default *p;
  1593. int i, ret;
  1594. bool bypass;
  1595. map->lock(map->lock_arg);
  1596. bypass = map->cache_bypass;
  1597. map->cache_bypass = true;
  1598. /* Write out first; it's useful to apply even if we fail later. */
  1599. for (i = 0; i < num_regs; i++) {
  1600. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1601. if (ret != 0) {
  1602. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  1603. regs[i].reg, regs[i].def, ret);
  1604. goto out;
  1605. }
  1606. }
  1607. p = krealloc(map->patch,
  1608. sizeof(struct reg_default) * (map->patch_regs + num_regs),
  1609. GFP_KERNEL);
  1610. if (p) {
  1611. memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
  1612. map->patch = p;
  1613. map->patch_regs += num_regs;
  1614. } else {
  1615. ret = -ENOMEM;
  1616. }
  1617. out:
  1618. map->cache_bypass = bypass;
  1619. map->unlock(map->lock_arg);
  1620. return ret;
  1621. }
  1622. EXPORT_SYMBOL_GPL(regmap_register_patch);
  1623. /*
  1624. * regmap_get_val_bytes(): Report the size of a register value
  1625. *
  1626. * Report the size of a register value, mainly intended to for use by
  1627. * generic infrastructure built on top of regmap.
  1628. */
  1629. int regmap_get_val_bytes(struct regmap *map)
  1630. {
  1631. if (map->format.format_write)
  1632. return -EINVAL;
  1633. return map->format.val_bytes;
  1634. }
  1635. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  1636. static int __init regmap_initcall(void)
  1637. {
  1638. regmap_debugfs_initcall();
  1639. return 0;
  1640. }
  1641. postcore_initcall(regmap_initcall);