regmap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/err.h>
  16. #include <linux/regmap.h>
  17. struct regmap;
  18. struct regmap_format {
  19. size_t buf_size;
  20. size_t reg_bytes;
  21. size_t val_bytes;
  22. void (*format_write)(struct regmap *map,
  23. unsigned int reg, unsigned int val);
  24. void (*format_reg)(void *buf, unsigned int reg);
  25. void (*format_val)(void *buf, unsigned int val);
  26. unsigned int (*parse_val)(void *buf);
  27. };
  28. struct regmap {
  29. struct mutex lock;
  30. struct device *dev; /* Device we do I/O on */
  31. void *work_buf; /* Scratch buffer used to format I/O */
  32. struct regmap_format format; /* Buffer format */
  33. const struct regmap_bus *bus;
  34. unsigned int max_register;
  35. bool (*writeable_reg)(struct device *dev, unsigned int reg);
  36. bool (*readable_reg)(struct device *dev, unsigned int reg);
  37. bool (*volatile_reg)(struct device *dev, unsigned int reg);
  38. };
  39. static void regmap_format_4_12_write(struct regmap *map,
  40. unsigned int reg, unsigned int val)
  41. {
  42. __be16 *out = map->work_buf;
  43. *out = cpu_to_be16((reg << 12) | val);
  44. }
  45. static void regmap_format_7_9_write(struct regmap *map,
  46. unsigned int reg, unsigned int val)
  47. {
  48. __be16 *out = map->work_buf;
  49. *out = cpu_to_be16((reg << 9) | val);
  50. }
  51. static void regmap_format_8(void *buf, unsigned int val)
  52. {
  53. u8 *b = buf;
  54. b[0] = val;
  55. }
  56. static void regmap_format_16(void *buf, unsigned int val)
  57. {
  58. __be16 *b = buf;
  59. b[0] = cpu_to_be16(val);
  60. }
  61. static unsigned int regmap_parse_8(void *buf)
  62. {
  63. u8 *b = buf;
  64. return b[0];
  65. }
  66. static unsigned int regmap_parse_16(void *buf)
  67. {
  68. __be16 *b = buf;
  69. b[0] = be16_to_cpu(b[0]);
  70. return b[0];
  71. }
  72. /**
  73. * regmap_init(): Initialise register map
  74. *
  75. * @dev: Device that will be interacted with
  76. * @bus: Bus-specific callbacks to use with device
  77. * @config: Configuration for register map
  78. *
  79. * The return value will be an ERR_PTR() on error or a valid pointer to
  80. * a struct regmap. This function should generally not be called
  81. * directly, it should be called by bus-specific init functions.
  82. */
  83. struct regmap *regmap_init(struct device *dev,
  84. const struct regmap_bus *bus,
  85. const struct regmap_config *config)
  86. {
  87. struct regmap *map;
  88. int ret = -EINVAL;
  89. if (!bus || !config)
  90. return NULL;
  91. map = kzalloc(sizeof(*map), GFP_KERNEL);
  92. if (map == NULL) {
  93. ret = -ENOMEM;
  94. goto err;
  95. }
  96. mutex_init(&map->lock);
  97. map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
  98. map->format.reg_bytes = config->reg_bits / 8;
  99. map->format.val_bytes = config->val_bits / 8;
  100. map->dev = dev;
  101. map->bus = bus;
  102. map->max_register = config->max_register;
  103. map->writeable_reg = config->writeable_reg;
  104. map->readable_reg = config->readable_reg;
  105. map->volatile_reg = config->volatile_reg;
  106. switch (config->reg_bits) {
  107. case 4:
  108. switch (config->val_bits) {
  109. case 12:
  110. map->format.format_write = regmap_format_4_12_write;
  111. break;
  112. default:
  113. goto err_map;
  114. }
  115. break;
  116. case 7:
  117. switch (config->val_bits) {
  118. case 9:
  119. map->format.format_write = regmap_format_7_9_write;
  120. break;
  121. default:
  122. goto err_map;
  123. }
  124. break;
  125. case 8:
  126. map->format.format_reg = regmap_format_8;
  127. break;
  128. case 16:
  129. map->format.format_reg = regmap_format_16;
  130. break;
  131. default:
  132. goto err_map;
  133. }
  134. switch (config->val_bits) {
  135. case 8:
  136. map->format.format_val = regmap_format_8;
  137. map->format.parse_val = regmap_parse_8;
  138. break;
  139. case 16:
  140. map->format.format_val = regmap_format_16;
  141. map->format.parse_val = regmap_parse_16;
  142. break;
  143. }
  144. if (!map->format.format_write &&
  145. !(map->format.format_reg && map->format.format_val))
  146. goto err_map;
  147. map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
  148. if (map->work_buf == NULL) {
  149. ret = -ENOMEM;
  150. goto err_bus;
  151. }
  152. return map;
  153. err_bus:
  154. module_put(map->bus->owner);
  155. err_map:
  156. kfree(map);
  157. err:
  158. return ERR_PTR(ret);
  159. }
  160. EXPORT_SYMBOL_GPL(regmap_init);
  161. /**
  162. * regmap_exit(): Free a previously allocated register map
  163. */
  164. void regmap_exit(struct regmap *map)
  165. {
  166. kfree(map->work_buf);
  167. module_put(map->bus->owner);
  168. kfree(map);
  169. }
  170. EXPORT_SYMBOL_GPL(regmap_exit);
  171. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  172. const void *val, size_t val_len)
  173. {
  174. void *buf;
  175. int ret = -ENOTSUPP;
  176. size_t len;
  177. map->format.format_reg(map->work_buf, reg);
  178. /* Try to do a gather write if we can */
  179. if (map->bus->gather_write)
  180. ret = map->bus->gather_write(map->dev, map->work_buf,
  181. map->format.reg_bytes,
  182. val, val_len);
  183. /* Otherwise fall back on linearising by hand. */
  184. if (ret == -ENOTSUPP) {
  185. len = map->format.reg_bytes + val_len;
  186. buf = kmalloc(len, GFP_KERNEL);
  187. if (!buf)
  188. return -ENOMEM;
  189. memcpy(buf, map->work_buf, map->format.reg_bytes);
  190. memcpy(buf + map->format.reg_bytes, val, val_len);
  191. ret = map->bus->write(map->dev, buf, len);
  192. kfree(buf);
  193. }
  194. return ret;
  195. }
  196. static int _regmap_write(struct regmap *map, unsigned int reg,
  197. unsigned int val)
  198. {
  199. BUG_ON(!map->format.format_write && !map->format.format_val);
  200. if (map->format.format_write) {
  201. map->format.format_write(map, reg, val);
  202. return map->bus->write(map->dev, map->work_buf,
  203. map->format.buf_size);
  204. } else {
  205. map->format.format_val(map->work_buf + map->format.reg_bytes,
  206. val);
  207. return _regmap_raw_write(map, reg,
  208. map->work_buf + map->format.reg_bytes,
  209. map->format.val_bytes);
  210. }
  211. }
  212. /**
  213. * regmap_write(): Write a value to a single register
  214. *
  215. * @map: Register map to write to
  216. * @reg: Register to write to
  217. * @val: Value to be written
  218. *
  219. * A value of zero will be returned on success, a negative errno will
  220. * be returned in error cases.
  221. */
  222. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  223. {
  224. int ret;
  225. mutex_lock(&map->lock);
  226. ret = _regmap_write(map, reg, val);
  227. mutex_unlock(&map->lock);
  228. return ret;
  229. }
  230. EXPORT_SYMBOL_GPL(regmap_write);
  231. /**
  232. * regmap_raw_write(): Write raw values to one or more registers
  233. *
  234. * @map: Register map to write to
  235. * @reg: Initial register to write to
  236. * @val: Block of data to be written, laid out for direct transmission to the
  237. * device
  238. * @val_len: Length of data pointed to by val.
  239. *
  240. * This function is intended to be used for things like firmware
  241. * download where a large block of data needs to be transferred to the
  242. * device. No formatting will be done on the data provided.
  243. *
  244. * A value of zero will be returned on success, a negative errno will
  245. * be returned in error cases.
  246. */
  247. int regmap_raw_write(struct regmap *map, unsigned int reg,
  248. const void *val, size_t val_len)
  249. {
  250. int ret;
  251. mutex_lock(&map->lock);
  252. ret = _regmap_raw_write(map, reg, val, val_len);
  253. mutex_unlock(&map->lock);
  254. return ret;
  255. }
  256. EXPORT_SYMBOL_GPL(regmap_raw_write);
  257. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  258. unsigned int val_len)
  259. {
  260. u8 *u8 = map->work_buf;
  261. int ret;
  262. map->format.format_reg(map->work_buf, reg);
  263. /*
  264. * Some buses flag reads by setting the high bits in the
  265. * register addresss; since it's always the high bits for all
  266. * current formats we can do this here rather than in
  267. * formatting. This may break if we get interesting formats.
  268. */
  269. if (map->bus->read_flag_mask)
  270. u8[0] |= map->bus->read_flag_mask;
  271. ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
  272. val, map->format.val_bytes);
  273. if (ret != 0)
  274. return ret;
  275. return 0;
  276. }
  277. static int _regmap_read(struct regmap *map, unsigned int reg,
  278. unsigned int *val)
  279. {
  280. int ret;
  281. if (!map->format.parse_val)
  282. return -EINVAL;
  283. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  284. if (ret == 0)
  285. *val = map->format.parse_val(map->work_buf);
  286. return ret;
  287. }
  288. /**
  289. * regmap_read(): Read a value from a single register
  290. *
  291. * @map: Register map to write to
  292. * @reg: Register to be read from
  293. * @val: Pointer to store read value
  294. *
  295. * A value of zero will be returned on success, a negative errno will
  296. * be returned in error cases.
  297. */
  298. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  299. {
  300. int ret;
  301. mutex_lock(&map->lock);
  302. ret = _regmap_read(map, reg, val);
  303. mutex_unlock(&map->lock);
  304. return ret;
  305. }
  306. EXPORT_SYMBOL_GPL(regmap_read);
  307. /**
  308. * regmap_raw_read(): Read raw data from the device
  309. *
  310. * @map: Register map to write to
  311. * @reg: First register to be read from
  312. * @val: Pointer to store read value
  313. * @val_len: Size of data to read
  314. *
  315. * A value of zero will be returned on success, a negative errno will
  316. * be returned in error cases.
  317. */
  318. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  319. size_t val_len)
  320. {
  321. int ret;
  322. mutex_lock(&map->lock);
  323. ret = _regmap_raw_read(map, reg, val, val_len);
  324. mutex_unlock(&map->lock);
  325. return ret;
  326. }
  327. EXPORT_SYMBOL_GPL(regmap_raw_read);
  328. /**
  329. * regmap_bulk_read(): Read multiple registers from the device
  330. *
  331. * @map: Register map to write to
  332. * @reg: First register to be read from
  333. * @val: Pointer to store read value, in native register size for device
  334. * @val_count: Number of registers to read
  335. *
  336. * A value of zero will be returned on success, a negative errno will
  337. * be returned in error cases.
  338. */
  339. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  340. size_t val_count)
  341. {
  342. int ret, i;
  343. size_t val_bytes = map->format.val_bytes;
  344. if (!map->format.parse_val)
  345. return -EINVAL;
  346. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  347. if (ret != 0)
  348. return ret;
  349. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  350. map->format.parse_val(val + i);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  354. /**
  355. * remap_update_bits: Perform a read/modify/write cycle on the register map
  356. *
  357. * @map: Register map to update
  358. * @reg: Register to update
  359. * @mask: Bitmask to change
  360. * @val: New value for bitmask
  361. *
  362. * Returns zero for success, a negative number on error.
  363. */
  364. int regmap_update_bits(struct regmap *map, unsigned int reg,
  365. unsigned int mask, unsigned int val)
  366. {
  367. int ret;
  368. unsigned int tmp;
  369. mutex_lock(&map->lock);
  370. ret = _regmap_read(map, reg, &tmp);
  371. if (ret != 0)
  372. goto out;
  373. tmp &= ~mask;
  374. tmp |= val & mask;
  375. ret = _regmap_write(map, reg, tmp);
  376. out:
  377. mutex_unlock(&map->lock);
  378. return ret;
  379. }
  380. EXPORT_SYMBOL_GPL(regmap_update_bits);