core.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * nvmem framework core.
  4. *
  5. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  6. * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/export.h>
  10. #include <linux/fs.h>
  11. #include <linux/idr.h>
  12. #include <linux/init.h>
  13. #include <linux/kref.h>
  14. #include <linux/module.h>
  15. #include <linux/nvmem-consumer.h>
  16. #include <linux/nvmem-provider.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/of.h>
  19. #include <linux/slab.h>
  20. #include "internals.h"
  21. #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  22. #define FLAG_COMPAT BIT(0)
  23. struct nvmem_cell_entry {
  24. const char *name;
  25. int offset;
  26. size_t raw_len;
  27. int bytes;
  28. int bit_offset;
  29. int nbits;
  30. nvmem_cell_post_process_t read_post_process;
  31. void *priv;
  32. struct device_node *np;
  33. struct nvmem_device *nvmem;
  34. struct list_head node;
  35. };
  36. struct nvmem_cell {
  37. struct nvmem_cell_entry *entry;
  38. const char *id;
  39. int index;
  40. };
  41. static DEFINE_MUTEX(nvmem_mutex);
  42. static DEFINE_IDA(nvmem_ida);
  43. static DEFINE_MUTEX(nvmem_lookup_mutex);
  44. static LIST_HEAD(nvmem_lookup_list);
  45. static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
  46. static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  47. void *val, size_t bytes)
  48. {
  49. if (nvmem->reg_read)
  50. return nvmem->reg_read(nvmem->priv, offset, val, bytes);
  51. return -EINVAL;
  52. }
  53. static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  54. void *val, size_t bytes)
  55. {
  56. int ret;
  57. if (nvmem->reg_write) {
  58. gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
  59. ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
  60. gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
  61. return ret;
  62. }
  63. return -EINVAL;
  64. }
  65. static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
  66. unsigned int offset, void *val,
  67. size_t bytes, int write)
  68. {
  69. unsigned int end = offset + bytes;
  70. unsigned int kend, ksize;
  71. const struct nvmem_keepout *keepout = nvmem->keepout;
  72. const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
  73. int rc;
  74. /*
  75. * Skip all keepouts before the range being accessed.
  76. * Keepouts are sorted.
  77. */
  78. while ((keepout < keepoutend) && (keepout->end <= offset))
  79. keepout++;
  80. while ((offset < end) && (keepout < keepoutend)) {
  81. /* Access the valid portion before the keepout. */
  82. if (offset < keepout->start) {
  83. kend = min(end, keepout->start);
  84. ksize = kend - offset;
  85. if (write)
  86. rc = __nvmem_reg_write(nvmem, offset, val, ksize);
  87. else
  88. rc = __nvmem_reg_read(nvmem, offset, val, ksize);
  89. if (rc)
  90. return rc;
  91. offset += ksize;
  92. val += ksize;
  93. }
  94. /*
  95. * Now we're aligned to the start of this keepout zone. Go
  96. * through it.
  97. */
  98. kend = min(end, keepout->end);
  99. ksize = kend - offset;
  100. if (!write)
  101. memset(val, keepout->value, ksize);
  102. val += ksize;
  103. offset += ksize;
  104. keepout++;
  105. }
  106. /*
  107. * If we ran out of keepouts but there's still stuff to do, send it
  108. * down directly
  109. */
  110. if (offset < end) {
  111. ksize = end - offset;
  112. if (write)
  113. return __nvmem_reg_write(nvmem, offset, val, ksize);
  114. else
  115. return __nvmem_reg_read(nvmem, offset, val, ksize);
  116. }
  117. return 0;
  118. }
  119. static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  120. void *val, size_t bytes)
  121. {
  122. if (!nvmem->nkeepout)
  123. return __nvmem_reg_read(nvmem, offset, val, bytes);
  124. return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
  125. }
  126. static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  127. void *val, size_t bytes)
  128. {
  129. if (!nvmem->nkeepout)
  130. return __nvmem_reg_write(nvmem, offset, val, bytes);
  131. return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
  132. }
  133. #ifdef CONFIG_NVMEM_SYSFS
  134. static const char * const nvmem_type_str[] = {
  135. [NVMEM_TYPE_UNKNOWN] = "Unknown",
  136. [NVMEM_TYPE_EEPROM] = "EEPROM",
  137. [NVMEM_TYPE_OTP] = "OTP",
  138. [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
  139. [NVMEM_TYPE_FRAM] = "FRAM",
  140. };
  141. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  142. static struct lock_class_key eeprom_lock_key;
  143. #endif
  144. static ssize_t type_show(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct nvmem_device *nvmem = to_nvmem_device(dev);
  148. return sysfs_emit(buf, "%s\n", nvmem_type_str[nvmem->type]);
  149. }
  150. static DEVICE_ATTR_RO(type);
  151. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  152. char *buf)
  153. {
  154. struct nvmem_device *nvmem = to_nvmem_device(dev);
  155. return sysfs_emit(buf, "%d\n", nvmem->read_only);
  156. }
  157. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  158. const char *buf, size_t count)
  159. {
  160. struct nvmem_device *nvmem = to_nvmem_device(dev);
  161. int ret = kstrtobool(buf, &nvmem->read_only);
  162. if (ret < 0)
  163. return ret;
  164. return count;
  165. }
  166. static DEVICE_ATTR_RW(force_ro);
  167. static struct attribute *nvmem_attrs[] = {
  168. &dev_attr_force_ro.attr,
  169. &dev_attr_type.attr,
  170. NULL,
  171. };
  172. static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
  173. const struct bin_attribute *attr, char *buf,
  174. loff_t pos, size_t count)
  175. {
  176. struct device *dev;
  177. struct nvmem_device *nvmem;
  178. int rc;
  179. if (attr->private)
  180. dev = attr->private;
  181. else
  182. dev = kobj_to_dev(kobj);
  183. nvmem = to_nvmem_device(dev);
  184. if (!IS_ALIGNED(pos, nvmem->stride))
  185. return -EINVAL;
  186. if (count < nvmem->word_size)
  187. return -EINVAL;
  188. count = round_down(count, nvmem->word_size);
  189. if (!nvmem->reg_read)
  190. return -EPERM;
  191. rc = nvmem_reg_read(nvmem, pos, buf, count);
  192. if (rc)
  193. return rc;
  194. return count;
  195. }
  196. static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
  197. const struct bin_attribute *attr, char *buf,
  198. loff_t pos, size_t count)
  199. {
  200. struct device *dev;
  201. struct nvmem_device *nvmem;
  202. int rc;
  203. if (attr->private)
  204. dev = attr->private;
  205. else
  206. dev = kobj_to_dev(kobj);
  207. nvmem = to_nvmem_device(dev);
  208. if (!IS_ALIGNED(pos, nvmem->stride))
  209. return -EINVAL;
  210. if (count < nvmem->word_size)
  211. return -EINVAL;
  212. count = round_down(count, nvmem->word_size);
  213. if (!nvmem->reg_write || nvmem->read_only)
  214. return -EPERM;
  215. rc = nvmem_reg_write(nvmem, pos, buf, count);
  216. if (rc)
  217. return rc;
  218. return count;
  219. }
  220. static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
  221. {
  222. umode_t mode = 0400;
  223. if (!nvmem->root_only)
  224. mode |= 0044;
  225. if (!nvmem->read_only)
  226. mode |= 0200;
  227. if (!nvmem->reg_write)
  228. mode &= ~0200;
  229. if (!nvmem->reg_read)
  230. mode &= ~0444;
  231. return mode;
  232. }
  233. static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
  234. const struct bin_attribute *attr,
  235. int i)
  236. {
  237. struct device *dev = kobj_to_dev(kobj);
  238. struct nvmem_device *nvmem = to_nvmem_device(dev);
  239. return nvmem_bin_attr_get_umode(nvmem);
  240. }
  241. static size_t nvmem_bin_attr_size(struct kobject *kobj,
  242. const struct bin_attribute *attr,
  243. int i)
  244. {
  245. struct device *dev = kobj_to_dev(kobj);
  246. struct nvmem_device *nvmem = to_nvmem_device(dev);
  247. return nvmem->size;
  248. }
  249. static umode_t nvmem_attr_is_visible(struct kobject *kobj,
  250. struct attribute *attr, int i)
  251. {
  252. struct device *dev = kobj_to_dev(kobj);
  253. struct nvmem_device *nvmem = to_nvmem_device(dev);
  254. /*
  255. * If the device has no .reg_write operation, do not allow
  256. * configuration as read-write.
  257. * If the device is set as read-only by configuration, it
  258. * can be forced into read-write mode using the 'force_ro'
  259. * attribute.
  260. */
  261. if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
  262. return 0; /* Attribute not visible */
  263. return attr->mode;
  264. }
  265. static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
  266. const char *id, int index);
  267. static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
  268. const struct bin_attribute *attr, char *buf,
  269. loff_t pos, size_t count)
  270. {
  271. struct nvmem_cell_entry *entry;
  272. struct nvmem_cell *cell = NULL;
  273. size_t cell_sz, read_len;
  274. void *content;
  275. entry = attr->private;
  276. cell = nvmem_create_cell(entry, entry->name, 0);
  277. if (IS_ERR(cell))
  278. return PTR_ERR(cell);
  279. if (!cell)
  280. return -EINVAL;
  281. content = nvmem_cell_read(cell, &cell_sz);
  282. if (IS_ERR(content)) {
  283. read_len = PTR_ERR(content);
  284. goto destroy_cell;
  285. }
  286. read_len = min_t(unsigned int, cell_sz - pos, count);
  287. memcpy(buf, content + pos, read_len);
  288. kfree(content);
  289. destroy_cell:
  290. kfree_const(cell->id);
  291. kfree(cell);
  292. return read_len;
  293. }
  294. /* default read/write permissions */
  295. static const struct bin_attribute bin_attr_rw_nvmem = {
  296. .attr = {
  297. .name = "nvmem",
  298. .mode = 0644,
  299. },
  300. .read = bin_attr_nvmem_read,
  301. .write = bin_attr_nvmem_write,
  302. };
  303. static const struct bin_attribute *const nvmem_bin_attributes[] = {
  304. &bin_attr_rw_nvmem,
  305. NULL,
  306. };
  307. static const struct attribute_group nvmem_bin_group = {
  308. .bin_attrs = nvmem_bin_attributes,
  309. .attrs = nvmem_attrs,
  310. .is_bin_visible = nvmem_bin_attr_is_visible,
  311. .bin_size = nvmem_bin_attr_size,
  312. .is_visible = nvmem_attr_is_visible,
  313. };
  314. static const struct attribute_group *nvmem_dev_groups[] = {
  315. &nvmem_bin_group,
  316. NULL,
  317. };
  318. static const struct bin_attribute bin_attr_nvmem_eeprom_compat = {
  319. .attr = {
  320. .name = "eeprom",
  321. },
  322. .read = bin_attr_nvmem_read,
  323. .write = bin_attr_nvmem_write,
  324. };
  325. /*
  326. * nvmem_setup_compat() - Create an additional binary entry in
  327. * drivers sys directory, to be backwards compatible with the older
  328. * drivers/misc/eeprom drivers.
  329. */
  330. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  331. const struct nvmem_config *config)
  332. {
  333. int rval;
  334. if (!config->compat)
  335. return 0;
  336. if (!config->base_dev)
  337. return -EINVAL;
  338. nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
  339. if (config->type == NVMEM_TYPE_FRAM)
  340. nvmem->eeprom.attr.name = "fram";
  341. nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
  342. nvmem->eeprom.size = nvmem->size;
  343. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  344. nvmem->eeprom.attr.key = &eeprom_lock_key;
  345. #endif
  346. nvmem->eeprom.private = &nvmem->dev;
  347. nvmem->base_dev = config->base_dev;
  348. rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
  349. if (rval) {
  350. dev_err(&nvmem->dev,
  351. "Failed to create eeprom binary file %d\n", rval);
  352. return rval;
  353. }
  354. nvmem->flags |= FLAG_COMPAT;
  355. return 0;
  356. }
  357. static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
  358. const struct nvmem_config *config)
  359. {
  360. if (config->compat)
  361. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  362. }
  363. static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
  364. {
  365. struct attribute_group group = {
  366. .name = "cells",
  367. };
  368. struct nvmem_cell_entry *entry;
  369. const struct bin_attribute **pattrs;
  370. struct bin_attribute *attrs;
  371. unsigned int ncells = 0, i = 0;
  372. int ret = 0;
  373. mutex_lock(&nvmem_mutex);
  374. if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
  375. goto unlock_mutex;
  376. /* Allocate an array of attributes with a sentinel */
  377. ncells = list_count_nodes(&nvmem->cells);
  378. pattrs = devm_kcalloc(&nvmem->dev, ncells + 1,
  379. sizeof(struct bin_attribute *), GFP_KERNEL);
  380. if (!pattrs) {
  381. ret = -ENOMEM;
  382. goto unlock_mutex;
  383. }
  384. attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
  385. if (!attrs) {
  386. ret = -ENOMEM;
  387. goto unlock_mutex;
  388. }
  389. /* Initialize each attribute to take the name and size of the cell */
  390. list_for_each_entry(entry, &nvmem->cells, node) {
  391. sysfs_bin_attr_init(&attrs[i]);
  392. attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
  393. "%s@%x,%x", entry->name,
  394. entry->offset,
  395. entry->bit_offset);
  396. attrs[i].attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem);
  397. attrs[i].size = entry->bytes;
  398. attrs[i].read = &nvmem_cell_attr_read;
  399. attrs[i].private = entry;
  400. if (!attrs[i].attr.name) {
  401. ret = -ENOMEM;
  402. goto unlock_mutex;
  403. }
  404. pattrs[i] = &attrs[i];
  405. i++;
  406. }
  407. group.bin_attrs = pattrs;
  408. ret = device_add_group(&nvmem->dev, &group);
  409. if (ret)
  410. goto unlock_mutex;
  411. nvmem->sysfs_cells_populated = true;
  412. unlock_mutex:
  413. mutex_unlock(&nvmem_mutex);
  414. return ret;
  415. }
  416. #else /* CONFIG_NVMEM_SYSFS */
  417. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  418. const struct nvmem_config *config)
  419. {
  420. return -ENOSYS;
  421. }
  422. static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
  423. const struct nvmem_config *config)
  424. {
  425. }
  426. #endif /* CONFIG_NVMEM_SYSFS */
  427. static void nvmem_release(struct device *dev)
  428. {
  429. struct nvmem_device *nvmem = to_nvmem_device(dev);
  430. ida_free(&nvmem_ida, nvmem->id);
  431. gpiod_put(nvmem->wp_gpio);
  432. kfree(nvmem);
  433. }
  434. static const struct device_type nvmem_provider_type = {
  435. .release = nvmem_release,
  436. };
  437. static const struct bus_type nvmem_bus_type = {
  438. .name = "nvmem",
  439. };
  440. static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
  441. {
  442. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
  443. mutex_lock(&nvmem_mutex);
  444. list_del(&cell->node);
  445. mutex_unlock(&nvmem_mutex);
  446. of_node_put(cell->np);
  447. kfree_const(cell->name);
  448. kfree(cell);
  449. }
  450. static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
  451. {
  452. struct nvmem_cell_entry *cell, *p;
  453. list_for_each_entry_safe(cell, p, &nvmem->cells, node)
  454. nvmem_cell_entry_drop(cell);
  455. }
  456. static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
  457. {
  458. mutex_lock(&nvmem_mutex);
  459. list_add_tail(&cell->node, &cell->nvmem->cells);
  460. mutex_unlock(&nvmem_mutex);
  461. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
  462. }
  463. static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
  464. const struct nvmem_cell_info *info,
  465. struct nvmem_cell_entry *cell)
  466. {
  467. cell->nvmem = nvmem;
  468. cell->offset = info->offset;
  469. cell->raw_len = info->raw_len ?: info->bytes;
  470. cell->bytes = info->bytes;
  471. cell->name = info->name;
  472. cell->read_post_process = info->read_post_process;
  473. cell->priv = info->priv;
  474. cell->bit_offset = info->bit_offset;
  475. cell->nbits = info->nbits;
  476. cell->np = info->np;
  477. if (cell->nbits) {
  478. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  479. BITS_PER_BYTE);
  480. cell->raw_len = ALIGN(cell->bytes, nvmem->word_size);
  481. }
  482. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  483. dev_err(&nvmem->dev,
  484. "cell %s unaligned to nvmem stride %d\n",
  485. cell->name ?: "<unknown>", nvmem->stride);
  486. return -EINVAL;
  487. }
  488. if (!IS_ALIGNED(cell->raw_len, nvmem->word_size)) {
  489. dev_err(&nvmem->dev,
  490. "cell %s raw len %zd unaligned to nvmem word size %d\n",
  491. cell->name ?: "<unknown>", cell->raw_len,
  492. nvmem->word_size);
  493. if (info->raw_len)
  494. return -EINVAL;
  495. cell->raw_len = ALIGN(cell->raw_len, nvmem->word_size);
  496. }
  497. return 0;
  498. }
  499. static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
  500. const struct nvmem_cell_info *info,
  501. struct nvmem_cell_entry *cell)
  502. {
  503. int err;
  504. err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
  505. if (err)
  506. return err;
  507. cell->name = kstrdup_const(info->name, GFP_KERNEL);
  508. if (!cell->name)
  509. return -ENOMEM;
  510. return 0;
  511. }
  512. /**
  513. * nvmem_add_one_cell() - Add one cell information to an nvmem device
  514. *
  515. * @nvmem: nvmem device to add cells to.
  516. * @info: nvmem cell info to add to the device
  517. *
  518. * Return: 0 or negative error code on failure.
  519. */
  520. int nvmem_add_one_cell(struct nvmem_device *nvmem,
  521. const struct nvmem_cell_info *info)
  522. {
  523. struct nvmem_cell_entry *cell;
  524. int rval;
  525. cell = kzalloc_obj(*cell);
  526. if (!cell)
  527. return -ENOMEM;
  528. rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
  529. if (rval) {
  530. kfree(cell);
  531. return rval;
  532. }
  533. nvmem_cell_entry_add(cell);
  534. return 0;
  535. }
  536. EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
  537. /**
  538. * nvmem_add_cells() - Add cell information to an nvmem device
  539. *
  540. * @nvmem: nvmem device to add cells to.
  541. * @info: nvmem cell info to add to the device
  542. * @ncells: number of cells in info
  543. *
  544. * Return: 0 or negative error code on failure.
  545. */
  546. static int nvmem_add_cells(struct nvmem_device *nvmem,
  547. const struct nvmem_cell_info *info,
  548. int ncells)
  549. {
  550. int i, rval;
  551. for (i = 0; i < ncells; i++) {
  552. rval = nvmem_add_one_cell(nvmem, &info[i]);
  553. if (rval)
  554. return rval;
  555. }
  556. return 0;
  557. }
  558. /**
  559. * nvmem_register_notifier() - Register a notifier block for nvmem events.
  560. *
  561. * @nb: notifier block to be called on nvmem events.
  562. *
  563. * Return: 0 on success, negative error number on failure.
  564. */
  565. int nvmem_register_notifier(struct notifier_block *nb)
  566. {
  567. return blocking_notifier_chain_register(&nvmem_notifier, nb);
  568. }
  569. EXPORT_SYMBOL_GPL(nvmem_register_notifier);
  570. /**
  571. * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
  572. *
  573. * @nb: notifier block to be unregistered.
  574. *
  575. * Return: 0 on success, negative error number on failure.
  576. */
  577. int nvmem_unregister_notifier(struct notifier_block *nb)
  578. {
  579. return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
  580. }
  581. EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
  582. static struct nvmem_cell_entry *
  583. nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
  584. {
  585. struct nvmem_cell_entry *iter, *cell = NULL;
  586. mutex_lock(&nvmem_mutex);
  587. list_for_each_entry(iter, &nvmem->cells, node) {
  588. if (strcmp(cell_id, iter->name) == 0) {
  589. cell = iter;
  590. break;
  591. }
  592. }
  593. mutex_unlock(&nvmem_mutex);
  594. return cell;
  595. }
  596. static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
  597. {
  598. unsigned int cur = 0;
  599. const struct nvmem_keepout *keepout = nvmem->keepout;
  600. const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
  601. while (keepout < keepoutend) {
  602. /* Ensure keepouts are sorted and don't overlap. */
  603. if (keepout->start < cur) {
  604. dev_err(&nvmem->dev,
  605. "Keepout regions aren't sorted or overlap.\n");
  606. return -ERANGE;
  607. }
  608. if (keepout->end < keepout->start) {
  609. dev_err(&nvmem->dev,
  610. "Invalid keepout region.\n");
  611. return -EINVAL;
  612. }
  613. /*
  614. * Validate keepouts (and holes between) don't violate
  615. * word_size constraints.
  616. */
  617. if ((keepout->end - keepout->start < nvmem->word_size) ||
  618. ((keepout->start != cur) &&
  619. (keepout->start - cur < nvmem->word_size))) {
  620. dev_err(&nvmem->dev,
  621. "Keepout regions violate word_size constraints.\n");
  622. return -ERANGE;
  623. }
  624. /* Validate keepouts don't violate stride (alignment). */
  625. if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
  626. !IS_ALIGNED(keepout->end, nvmem->stride)) {
  627. dev_err(&nvmem->dev,
  628. "Keepout regions violate stride.\n");
  629. return -EINVAL;
  630. }
  631. cur = keepout->end;
  632. keepout++;
  633. }
  634. return 0;
  635. }
  636. static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
  637. {
  638. struct device *dev = &nvmem->dev;
  639. const __be32 *addr;
  640. int len, ret;
  641. for_each_child_of_node_scoped(np, child) {
  642. struct nvmem_cell_info info = {0};
  643. addr = of_get_property(child, "reg", &len);
  644. if (!addr)
  645. continue;
  646. if (len < 2 * sizeof(u32)) {
  647. dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
  648. return -EINVAL;
  649. }
  650. info.offset = be32_to_cpup(addr++);
  651. info.bytes = be32_to_cpup(addr);
  652. info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
  653. addr = of_get_property(child, "bits", &len);
  654. if (addr && len == (2 * sizeof(u32))) {
  655. info.bit_offset = be32_to_cpup(addr++);
  656. info.nbits = be32_to_cpup(addr);
  657. if (info.bit_offset >= BITS_PER_BYTE * info.bytes ||
  658. info.nbits < 1 ||
  659. info.bit_offset + info.nbits > BITS_PER_BYTE * info.bytes) {
  660. dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
  661. return -EINVAL;
  662. }
  663. }
  664. info.np = of_node_get(child);
  665. if (nvmem->fixup_dt_cell_info)
  666. nvmem->fixup_dt_cell_info(nvmem, &info);
  667. ret = nvmem_add_one_cell(nvmem, &info);
  668. kfree(info.name);
  669. if (ret) {
  670. of_node_put(info.np);
  671. return ret;
  672. }
  673. }
  674. return 0;
  675. }
  676. static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
  677. {
  678. return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
  679. }
  680. static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem)
  681. {
  682. struct device_node *layout_np;
  683. int err = 0;
  684. layout_np = of_nvmem_layout_get_container(nvmem);
  685. if (!layout_np)
  686. return 0;
  687. if (of_device_is_compatible(layout_np, "fixed-layout"))
  688. err = nvmem_add_cells_from_dt(nvmem, layout_np);
  689. of_node_put(layout_np);
  690. return err;
  691. }
  692. int nvmem_layout_register(struct nvmem_layout *layout)
  693. {
  694. int ret;
  695. if (!layout->add_cells)
  696. return -EINVAL;
  697. /* Populate the cells */
  698. ret = layout->add_cells(layout);
  699. if (ret)
  700. return ret;
  701. #ifdef CONFIG_NVMEM_SYSFS
  702. ret = nvmem_populate_sysfs_cells(layout->nvmem);
  703. if (ret) {
  704. nvmem_device_remove_all_cells(layout->nvmem);
  705. return ret;
  706. }
  707. #endif
  708. return 0;
  709. }
  710. EXPORT_SYMBOL_GPL(nvmem_layout_register);
  711. void nvmem_layout_unregister(struct nvmem_layout *layout)
  712. {
  713. /* Keep the API even with an empty stub in case we need it later */
  714. }
  715. EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
  716. /**
  717. * nvmem_register() - Register a nvmem device for given nvmem_config.
  718. * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  719. *
  720. * @config: nvmem device configuration with which nvmem device is created.
  721. *
  722. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  723. * on success.
  724. */
  725. struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  726. {
  727. struct nvmem_device *nvmem;
  728. int rval;
  729. if (!config->dev)
  730. return ERR_PTR(-EINVAL);
  731. if (!config->reg_read && !config->reg_write)
  732. return ERR_PTR(-EINVAL);
  733. nvmem = kzalloc_obj(*nvmem);
  734. if (!nvmem)
  735. return ERR_PTR(-ENOMEM);
  736. rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
  737. if (rval < 0) {
  738. kfree(nvmem);
  739. return ERR_PTR(rval);
  740. }
  741. nvmem->id = rval;
  742. nvmem->dev.type = &nvmem_provider_type;
  743. nvmem->dev.bus = &nvmem_bus_type;
  744. nvmem->dev.parent = config->dev;
  745. device_initialize(&nvmem->dev);
  746. if (!config->ignore_wp)
  747. nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
  748. GPIOD_OUT_HIGH);
  749. if (IS_ERR(nvmem->wp_gpio)) {
  750. rval = PTR_ERR(nvmem->wp_gpio);
  751. nvmem->wp_gpio = NULL;
  752. goto err_put_device;
  753. }
  754. kref_init(&nvmem->refcnt);
  755. INIT_LIST_HEAD(&nvmem->cells);
  756. nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
  757. nvmem->owner = config->owner;
  758. if (!nvmem->owner && config->dev->driver)
  759. nvmem->owner = config->dev->driver->owner;
  760. nvmem->stride = config->stride ?: 1;
  761. nvmem->word_size = config->word_size ?: 1;
  762. nvmem->size = config->size;
  763. nvmem->root_only = config->root_only;
  764. nvmem->priv = config->priv;
  765. nvmem->type = config->type;
  766. nvmem->reg_read = config->reg_read;
  767. nvmem->reg_write = config->reg_write;
  768. nvmem->keepout = config->keepout;
  769. nvmem->nkeepout = config->nkeepout;
  770. if (config->of_node)
  771. nvmem->dev.of_node = config->of_node;
  772. else
  773. nvmem->dev.of_node = config->dev->of_node;
  774. switch (config->id) {
  775. case NVMEM_DEVID_NONE:
  776. rval = dev_set_name(&nvmem->dev, "%s", config->name);
  777. break;
  778. case NVMEM_DEVID_AUTO:
  779. rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
  780. break;
  781. default:
  782. rval = dev_set_name(&nvmem->dev, "%s%d",
  783. config->name ? : "nvmem",
  784. config->name ? config->id : nvmem->id);
  785. break;
  786. }
  787. if (rval)
  788. goto err_put_device;
  789. nvmem->read_only = device_property_present(config->dev, "read-only") ||
  790. config->read_only || !nvmem->reg_write;
  791. #ifdef CONFIG_NVMEM_SYSFS
  792. nvmem->dev.groups = nvmem_dev_groups;
  793. #endif
  794. if (nvmem->nkeepout) {
  795. rval = nvmem_validate_keepouts(nvmem);
  796. if (rval)
  797. goto err_put_device;
  798. }
  799. if (config->compat) {
  800. rval = nvmem_sysfs_setup_compat(nvmem, config);
  801. if (rval)
  802. goto err_put_device;
  803. }
  804. if (config->cells) {
  805. rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
  806. if (rval)
  807. goto err_remove_cells;
  808. }
  809. if (config->add_legacy_fixed_of_cells) {
  810. rval = nvmem_add_cells_from_legacy_of(nvmem);
  811. if (rval)
  812. goto err_remove_cells;
  813. }
  814. rval = nvmem_add_cells_from_fixed_layout(nvmem);
  815. if (rval)
  816. goto err_remove_cells;
  817. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  818. rval = device_add(&nvmem->dev);
  819. if (rval)
  820. goto err_remove_cells;
  821. rval = nvmem_populate_layout(nvmem);
  822. if (rval)
  823. goto err_remove_dev;
  824. #ifdef CONFIG_NVMEM_SYSFS
  825. rval = nvmem_populate_sysfs_cells(nvmem);
  826. if (rval)
  827. goto err_destroy_layout;
  828. #endif
  829. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
  830. return nvmem;
  831. #ifdef CONFIG_NVMEM_SYSFS
  832. err_destroy_layout:
  833. nvmem_destroy_layout(nvmem);
  834. #endif
  835. err_remove_dev:
  836. device_del(&nvmem->dev);
  837. err_remove_cells:
  838. nvmem_device_remove_all_cells(nvmem);
  839. if (config->compat)
  840. nvmem_sysfs_remove_compat(nvmem, config);
  841. err_put_device:
  842. put_device(&nvmem->dev);
  843. return ERR_PTR(rval);
  844. }
  845. EXPORT_SYMBOL_GPL(nvmem_register);
  846. static void nvmem_device_release(struct kref *kref)
  847. {
  848. struct nvmem_device *nvmem;
  849. nvmem = container_of(kref, struct nvmem_device, refcnt);
  850. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
  851. if (nvmem->flags & FLAG_COMPAT)
  852. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  853. nvmem_device_remove_all_cells(nvmem);
  854. nvmem_destroy_layout(nvmem);
  855. device_unregister(&nvmem->dev);
  856. }
  857. /**
  858. * nvmem_unregister() - Unregister previously registered nvmem device
  859. *
  860. * @nvmem: Pointer to previously registered nvmem device.
  861. */
  862. void nvmem_unregister(struct nvmem_device *nvmem)
  863. {
  864. if (nvmem)
  865. kref_put(&nvmem->refcnt, nvmem_device_release);
  866. }
  867. EXPORT_SYMBOL_GPL(nvmem_unregister);
  868. static void devm_nvmem_unregister(void *nvmem)
  869. {
  870. nvmem_unregister(nvmem);
  871. }
  872. /**
  873. * devm_nvmem_register() - Register a managed nvmem device for given
  874. * nvmem_config.
  875. * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  876. *
  877. * @dev: Device that uses the nvmem device.
  878. * @config: nvmem device configuration with which nvmem device is created.
  879. *
  880. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  881. * on success.
  882. */
  883. struct nvmem_device *devm_nvmem_register(struct device *dev,
  884. const struct nvmem_config *config)
  885. {
  886. struct nvmem_device *nvmem;
  887. int ret;
  888. nvmem = nvmem_register(config);
  889. if (IS_ERR(nvmem))
  890. return nvmem;
  891. ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
  892. if (ret)
  893. return ERR_PTR(ret);
  894. return nvmem;
  895. }
  896. EXPORT_SYMBOL_GPL(devm_nvmem_register);
  897. static struct nvmem_device *__nvmem_device_get(void *data,
  898. int (*match)(struct device *dev, const void *data))
  899. {
  900. struct nvmem_device *nvmem = NULL;
  901. struct device *dev;
  902. mutex_lock(&nvmem_mutex);
  903. dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
  904. if (dev)
  905. nvmem = to_nvmem_device(dev);
  906. mutex_unlock(&nvmem_mutex);
  907. if (!nvmem)
  908. return ERR_PTR(-EPROBE_DEFER);
  909. if (!try_module_get(nvmem->owner)) {
  910. dev_err(&nvmem->dev,
  911. "could not increase module refcount for cell %s\n",
  912. nvmem_dev_name(nvmem));
  913. put_device(&nvmem->dev);
  914. return ERR_PTR(-EINVAL);
  915. }
  916. kref_get(&nvmem->refcnt);
  917. return nvmem;
  918. }
  919. static void __nvmem_device_put(struct nvmem_device *nvmem)
  920. {
  921. put_device(&nvmem->dev);
  922. module_put(nvmem->owner);
  923. kref_put(&nvmem->refcnt, nvmem_device_release);
  924. }
  925. #if IS_ENABLED(CONFIG_OF)
  926. /**
  927. * of_nvmem_device_get() - Get nvmem device from a given id
  928. *
  929. * @np: Device tree node that uses the nvmem device.
  930. * @id: nvmem name from nvmem-names property.
  931. *
  932. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  933. * on success.
  934. */
  935. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  936. {
  937. struct device_node *nvmem_np;
  938. struct nvmem_device *nvmem;
  939. int index = 0;
  940. if (id)
  941. index = of_property_match_string(np, "nvmem-names", id);
  942. nvmem_np = of_parse_phandle(np, "nvmem", index);
  943. if (!nvmem_np)
  944. return ERR_PTR(-ENOENT);
  945. nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
  946. of_node_put(nvmem_np);
  947. return nvmem;
  948. }
  949. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  950. #endif
  951. /**
  952. * nvmem_device_get() - Get nvmem device from a given id
  953. *
  954. * @dev: Device that uses the nvmem device.
  955. * @dev_name: name of the requested nvmem device.
  956. *
  957. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  958. * on success.
  959. */
  960. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  961. {
  962. if (dev->of_node) { /* try dt first */
  963. struct nvmem_device *nvmem;
  964. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  965. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  966. return nvmem;
  967. }
  968. return __nvmem_device_get((void *)dev_name, device_match_name);
  969. }
  970. EXPORT_SYMBOL_GPL(nvmem_device_get);
  971. /**
  972. * nvmem_device_find() - Find nvmem device with matching function
  973. *
  974. * @data: Data to pass to match function
  975. * @match: Callback function to check device
  976. *
  977. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  978. * on success.
  979. */
  980. struct nvmem_device *nvmem_device_find(void *data,
  981. int (*match)(struct device *dev, const void *data))
  982. {
  983. return __nvmem_device_get(data, match);
  984. }
  985. EXPORT_SYMBOL_GPL(nvmem_device_find);
  986. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  987. {
  988. struct nvmem_device **nvmem = res;
  989. if (WARN_ON(!nvmem || !*nvmem))
  990. return 0;
  991. return *nvmem == data;
  992. }
  993. static void devm_nvmem_device_release(struct device *dev, void *res)
  994. {
  995. nvmem_device_put(*(struct nvmem_device **)res);
  996. }
  997. /**
  998. * devm_nvmem_device_put() - put already got nvmem device
  999. *
  1000. * @dev: Device that uses the nvmem device.
  1001. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  1002. * that needs to be released.
  1003. */
  1004. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  1005. {
  1006. int ret;
  1007. ret = devres_release(dev, devm_nvmem_device_release,
  1008. devm_nvmem_device_match, nvmem);
  1009. WARN_ON(ret);
  1010. }
  1011. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  1012. /**
  1013. * nvmem_device_put() - put already got nvmem device
  1014. *
  1015. * @nvmem: pointer to nvmem device that needs to be released.
  1016. */
  1017. void nvmem_device_put(struct nvmem_device *nvmem)
  1018. {
  1019. __nvmem_device_put(nvmem);
  1020. }
  1021. EXPORT_SYMBOL_GPL(nvmem_device_put);
  1022. /**
  1023. * devm_nvmem_device_get() - Get nvmem device of device from a given id
  1024. *
  1025. * @dev: Device that requests the nvmem device.
  1026. * @id: name id for the requested nvmem device.
  1027. *
  1028. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  1029. * on success. The nvmem_device will be freed by the automatically once the
  1030. * device is freed.
  1031. */
  1032. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  1033. {
  1034. struct nvmem_device **ptr, *nvmem;
  1035. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  1036. if (!ptr)
  1037. return ERR_PTR(-ENOMEM);
  1038. nvmem = nvmem_device_get(dev, id);
  1039. if (!IS_ERR(nvmem)) {
  1040. *ptr = nvmem;
  1041. devres_add(dev, ptr);
  1042. } else {
  1043. devres_free(ptr);
  1044. }
  1045. return nvmem;
  1046. }
  1047. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  1048. static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
  1049. const char *id, int index)
  1050. {
  1051. struct nvmem_cell *cell;
  1052. const char *name = NULL;
  1053. cell = kzalloc_obj(*cell);
  1054. if (!cell)
  1055. return ERR_PTR(-ENOMEM);
  1056. if (id) {
  1057. name = kstrdup_const(id, GFP_KERNEL);
  1058. if (!name) {
  1059. kfree(cell);
  1060. return ERR_PTR(-ENOMEM);
  1061. }
  1062. }
  1063. cell->id = name;
  1064. cell->entry = entry;
  1065. cell->index = index;
  1066. return cell;
  1067. }
  1068. static struct nvmem_cell *
  1069. nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
  1070. {
  1071. struct nvmem_cell_entry *cell_entry;
  1072. struct nvmem_cell *cell = ERR_PTR(-ENOENT);
  1073. struct nvmem_cell_lookup *lookup;
  1074. struct nvmem_device *nvmem;
  1075. const char *dev_id;
  1076. if (!dev)
  1077. return ERR_PTR(-EINVAL);
  1078. dev_id = dev_name(dev);
  1079. mutex_lock(&nvmem_lookup_mutex);
  1080. list_for_each_entry(lookup, &nvmem_lookup_list, node) {
  1081. if ((strcmp(lookup->dev_id, dev_id) == 0) &&
  1082. (strcmp(lookup->con_id, con_id) == 0)) {
  1083. /* This is the right entry. */
  1084. nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
  1085. device_match_name);
  1086. if (IS_ERR(nvmem)) {
  1087. /* Provider may not be registered yet. */
  1088. cell = ERR_CAST(nvmem);
  1089. break;
  1090. }
  1091. cell_entry = nvmem_find_cell_entry_by_name(nvmem,
  1092. lookup->cell_name);
  1093. if (!cell_entry) {
  1094. __nvmem_device_put(nvmem);
  1095. cell = ERR_PTR(-ENOENT);
  1096. } else {
  1097. cell = nvmem_create_cell(cell_entry, con_id, 0);
  1098. if (IS_ERR(cell))
  1099. __nvmem_device_put(nvmem);
  1100. }
  1101. break;
  1102. }
  1103. }
  1104. mutex_unlock(&nvmem_lookup_mutex);
  1105. return cell;
  1106. }
  1107. static void nvmem_layout_module_put(struct nvmem_device *nvmem)
  1108. {
  1109. if (nvmem->layout && nvmem->layout->dev.driver)
  1110. module_put(nvmem->layout->dev.driver->owner);
  1111. }
  1112. #if IS_ENABLED(CONFIG_OF)
  1113. static struct nvmem_cell_entry *
  1114. nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
  1115. {
  1116. struct nvmem_cell_entry *iter, *cell = NULL;
  1117. mutex_lock(&nvmem_mutex);
  1118. list_for_each_entry(iter, &nvmem->cells, node) {
  1119. if (np == iter->np) {
  1120. cell = iter;
  1121. break;
  1122. }
  1123. }
  1124. mutex_unlock(&nvmem_mutex);
  1125. return cell;
  1126. }
  1127. static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
  1128. {
  1129. if (!nvmem->layout)
  1130. return 0;
  1131. if (!nvmem->layout->dev.driver ||
  1132. !try_module_get(nvmem->layout->dev.driver->owner))
  1133. return -EPROBE_DEFER;
  1134. return 0;
  1135. }
  1136. /**
  1137. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  1138. *
  1139. * @np: Device tree node that uses the nvmem cell.
  1140. * @id: nvmem cell name from nvmem-cell-names property, or NULL
  1141. * for the cell at index 0 (the lone cell with no accompanying
  1142. * nvmem-cell-names property).
  1143. *
  1144. * Return: Will be an ERR_PTR() on error or a valid pointer
  1145. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  1146. * nvmem_cell_put().
  1147. */
  1148. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
  1149. {
  1150. struct device_node *cell_np, *nvmem_np;
  1151. struct nvmem_device *nvmem;
  1152. struct nvmem_cell_entry *cell_entry;
  1153. struct nvmem_cell *cell;
  1154. struct of_phandle_args cell_spec;
  1155. int index = 0;
  1156. int cell_index = 0;
  1157. int ret;
  1158. /* if cell name exists, find index to the name */
  1159. if (id)
  1160. index = of_property_match_string(np, "nvmem-cell-names", id);
  1161. ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
  1162. "#nvmem-cell-cells",
  1163. index, &cell_spec);
  1164. if (ret)
  1165. return ERR_PTR(-ENOENT);
  1166. if (cell_spec.args_count > 1)
  1167. return ERR_PTR(-EINVAL);
  1168. cell_np = cell_spec.np;
  1169. if (cell_spec.args_count)
  1170. cell_index = cell_spec.args[0];
  1171. nvmem_np = of_get_parent(cell_np);
  1172. if (!nvmem_np) {
  1173. of_node_put(cell_np);
  1174. return ERR_PTR(-EINVAL);
  1175. }
  1176. /* nvmem layouts produce cells within the nvmem-layout container */
  1177. if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
  1178. nvmem_np = of_get_next_parent(nvmem_np);
  1179. if (!nvmem_np) {
  1180. of_node_put(cell_np);
  1181. return ERR_PTR(-EINVAL);
  1182. }
  1183. }
  1184. nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
  1185. of_node_put(nvmem_np);
  1186. if (IS_ERR(nvmem)) {
  1187. of_node_put(cell_np);
  1188. return ERR_CAST(nvmem);
  1189. }
  1190. ret = nvmem_layout_module_get_optional(nvmem);
  1191. if (ret) {
  1192. of_node_put(cell_np);
  1193. __nvmem_device_put(nvmem);
  1194. return ERR_PTR(ret);
  1195. }
  1196. cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
  1197. of_node_put(cell_np);
  1198. if (!cell_entry) {
  1199. __nvmem_device_put(nvmem);
  1200. nvmem_layout_module_put(nvmem);
  1201. if (nvmem->layout)
  1202. return ERR_PTR(-EPROBE_DEFER);
  1203. else
  1204. return ERR_PTR(-ENOENT);
  1205. }
  1206. cell = nvmem_create_cell(cell_entry, id, cell_index);
  1207. if (IS_ERR(cell)) {
  1208. __nvmem_device_put(nvmem);
  1209. nvmem_layout_module_put(nvmem);
  1210. }
  1211. return cell;
  1212. }
  1213. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  1214. #endif
  1215. /**
  1216. * nvmem_cell_get() - Get nvmem cell of device from a given cell name
  1217. *
  1218. * @dev: Device that requests the nvmem cell.
  1219. * @id: nvmem cell name to get (this corresponds with the name from the
  1220. * nvmem-cell-names property for DT systems and with the con_id from
  1221. * the lookup entry for non-DT systems).
  1222. *
  1223. * Return: Will be an ERR_PTR() on error or a valid pointer
  1224. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  1225. * nvmem_cell_put().
  1226. */
  1227. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
  1228. {
  1229. struct nvmem_cell *cell;
  1230. if (dev->of_node) { /* try dt first */
  1231. cell = of_nvmem_cell_get(dev->of_node, id);
  1232. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  1233. return cell;
  1234. }
  1235. /* NULL cell id only allowed for device tree; invalid otherwise */
  1236. if (!id)
  1237. return ERR_PTR(-EINVAL);
  1238. return nvmem_cell_get_from_lookup(dev, id);
  1239. }
  1240. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  1241. static void devm_nvmem_cell_release(struct device *dev, void *res)
  1242. {
  1243. nvmem_cell_put(*(struct nvmem_cell **)res);
  1244. }
  1245. /**
  1246. * devm_nvmem_cell_get() - Get nvmem cell of device from a given id
  1247. *
  1248. * @dev: Device that requests the nvmem cell.
  1249. * @id: nvmem cell name id to get.
  1250. *
  1251. * Return: Will be an ERR_PTR() on error or a valid pointer
  1252. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  1253. * automatically once the device is freed.
  1254. */
  1255. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  1256. {
  1257. struct nvmem_cell **ptr, *cell;
  1258. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  1259. if (!ptr)
  1260. return ERR_PTR(-ENOMEM);
  1261. cell = nvmem_cell_get(dev, id);
  1262. if (!IS_ERR(cell)) {
  1263. *ptr = cell;
  1264. devres_add(dev, ptr);
  1265. } else {
  1266. devres_free(ptr);
  1267. }
  1268. return cell;
  1269. }
  1270. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  1271. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  1272. {
  1273. struct nvmem_cell **c = res;
  1274. if (WARN_ON(!c || !*c))
  1275. return 0;
  1276. return *c == data;
  1277. }
  1278. /**
  1279. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  1280. * from devm_nvmem_cell_get.
  1281. *
  1282. * @dev: Device that requests the nvmem cell.
  1283. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
  1284. */
  1285. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  1286. {
  1287. int ret;
  1288. ret = devres_release(dev, devm_nvmem_cell_release,
  1289. devm_nvmem_cell_match, cell);
  1290. WARN_ON(ret);
  1291. }
  1292. EXPORT_SYMBOL(devm_nvmem_cell_put);
  1293. /**
  1294. * nvmem_cell_put() - Release previously allocated nvmem cell.
  1295. *
  1296. * @cell: Previously allocated nvmem cell by nvmem_cell_get().
  1297. */
  1298. void nvmem_cell_put(struct nvmem_cell *cell)
  1299. {
  1300. struct nvmem_device *nvmem = cell->entry->nvmem;
  1301. if (cell->id)
  1302. kfree_const(cell->id);
  1303. kfree(cell);
  1304. __nvmem_device_put(nvmem);
  1305. nvmem_layout_module_put(nvmem);
  1306. }
  1307. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  1308. static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
  1309. {
  1310. u8 *p, *b;
  1311. int i, extra, bytes_offset;
  1312. int bit_offset = cell->bit_offset;
  1313. p = b = buf;
  1314. bytes_offset = bit_offset / BITS_PER_BYTE;
  1315. b += bytes_offset;
  1316. bit_offset %= BITS_PER_BYTE;
  1317. if (bit_offset % BITS_PER_BYTE) {
  1318. /* First shift */
  1319. *p = *b++ >> bit_offset;
  1320. /* setup rest of the bytes if any */
  1321. for (i = 1; i < cell->bytes; i++) {
  1322. /* Get bits from next byte and shift them towards msb */
  1323. *p++ |= *b << (BITS_PER_BYTE - bit_offset);
  1324. *p = *b++ >> bit_offset;
  1325. }
  1326. } else if (p != b) {
  1327. memmove(p, b, cell->bytes - bytes_offset);
  1328. p += cell->bytes - 1;
  1329. } else {
  1330. /* point to the msb */
  1331. p += cell->bytes - 1;
  1332. }
  1333. /* result fits in less bytes */
  1334. extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
  1335. while (--extra >= 0)
  1336. *p-- = 0;
  1337. /* clear msb bits if any leftover in the last byte */
  1338. if (cell->nbits % BITS_PER_BYTE)
  1339. *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
  1340. }
  1341. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  1342. struct nvmem_cell_entry *cell,
  1343. void *buf, size_t *len, const char *id, int index)
  1344. {
  1345. int rc;
  1346. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
  1347. if (rc)
  1348. return rc;
  1349. /* shift bits in-place */
  1350. if (cell->bit_offset || cell->nbits)
  1351. nvmem_shift_read_buffer_in_place(cell, buf);
  1352. if (cell->read_post_process) {
  1353. rc = cell->read_post_process(cell->priv, id, index,
  1354. cell->offset, buf, cell->raw_len);
  1355. if (rc)
  1356. return rc;
  1357. }
  1358. if (len)
  1359. *len = cell->bytes;
  1360. return 0;
  1361. }
  1362. /**
  1363. * nvmem_cell_read() - Read a given nvmem cell
  1364. *
  1365. * @cell: nvmem cell to be read.
  1366. * @len: pointer to length of cell which will be populated on successful read;
  1367. * can be NULL.
  1368. *
  1369. * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
  1370. * buffer should be freed by the consumer with a kfree().
  1371. */
  1372. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  1373. {
  1374. struct nvmem_cell_entry *entry = cell->entry;
  1375. struct nvmem_device *nvmem = entry->nvmem;
  1376. u8 *buf;
  1377. int rc;
  1378. if (!nvmem)
  1379. return ERR_PTR(-EINVAL);
  1380. buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL);
  1381. if (!buf)
  1382. return ERR_PTR(-ENOMEM);
  1383. rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
  1384. if (rc) {
  1385. kfree(buf);
  1386. return ERR_PTR(rc);
  1387. }
  1388. return buf;
  1389. }
  1390. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  1391. static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
  1392. u8 *_buf, int len)
  1393. {
  1394. struct nvmem_device *nvmem = cell->nvmem;
  1395. int i, rc, nbits, bit_offset = cell->bit_offset;
  1396. u8 v, *p, *buf, *b, pbyte, pbits;
  1397. nbits = cell->nbits;
  1398. buf = kzalloc(cell->bytes, GFP_KERNEL);
  1399. if (!buf)
  1400. return ERR_PTR(-ENOMEM);
  1401. memcpy(buf, _buf, len);
  1402. p = b = buf;
  1403. if (bit_offset) {
  1404. pbyte = *b;
  1405. *b <<= bit_offset;
  1406. /* setup the first byte with lsb bits from nvmem */
  1407. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  1408. if (rc)
  1409. goto err;
  1410. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  1411. /* setup rest of the byte if any */
  1412. for (i = 1; i < cell->bytes; i++) {
  1413. /* Get last byte bits and shift them towards lsb */
  1414. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  1415. pbyte = *b;
  1416. p = b;
  1417. *b <<= bit_offset;
  1418. *b++ |= pbits;
  1419. }
  1420. }
  1421. /* if it's not end on byte boundary */
  1422. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  1423. /* setup the last byte with msb bits from nvmem */
  1424. rc = nvmem_reg_read(nvmem,
  1425. cell->offset + cell->bytes - 1, &v, 1);
  1426. if (rc)
  1427. goto err;
  1428. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  1429. }
  1430. return buf;
  1431. err:
  1432. kfree(buf);
  1433. return ERR_PTR(rc);
  1434. }
  1435. static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
  1436. {
  1437. struct nvmem_device *nvmem = cell->nvmem;
  1438. int rc;
  1439. if (!nvmem || nvmem->read_only ||
  1440. (cell->bit_offset == 0 && len != cell->bytes))
  1441. return -EINVAL;
  1442. /*
  1443. * Any cells which have a read_post_process hook are read-only because
  1444. * we cannot reverse the operation and it might affect other cells,
  1445. * too.
  1446. */
  1447. if (cell->read_post_process)
  1448. return -EINVAL;
  1449. if (cell->bit_offset || cell->nbits) {
  1450. if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes)
  1451. return -EINVAL;
  1452. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  1453. if (IS_ERR(buf))
  1454. return PTR_ERR(buf);
  1455. }
  1456. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  1457. /* free the tmp buffer */
  1458. if (cell->bit_offset || cell->nbits)
  1459. kfree(buf);
  1460. if (rc)
  1461. return rc;
  1462. return len;
  1463. }
  1464. /**
  1465. * nvmem_cell_write() - Write to a given nvmem cell
  1466. *
  1467. * @cell: nvmem cell to be written.
  1468. * @buf: Buffer to be written.
  1469. * @len: length of buffer to be written to nvmem cell.
  1470. *
  1471. * Return: length of bytes written or negative on failure.
  1472. */
  1473. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  1474. {
  1475. return __nvmem_cell_entry_write(cell->entry, buf, len);
  1476. }
  1477. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  1478. static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
  1479. void *val, size_t count)
  1480. {
  1481. struct nvmem_cell *cell;
  1482. void *buf;
  1483. size_t len;
  1484. cell = nvmem_cell_get(dev, cell_id);
  1485. if (IS_ERR(cell))
  1486. return PTR_ERR(cell);
  1487. buf = nvmem_cell_read(cell, &len);
  1488. if (IS_ERR(buf)) {
  1489. nvmem_cell_put(cell);
  1490. return PTR_ERR(buf);
  1491. }
  1492. if (len != count) {
  1493. kfree(buf);
  1494. nvmem_cell_put(cell);
  1495. return -EINVAL;
  1496. }
  1497. memcpy(val, buf, count);
  1498. kfree(buf);
  1499. nvmem_cell_put(cell);
  1500. return 0;
  1501. }
  1502. /**
  1503. * nvmem_cell_read_u8() - Read a cell value as a u8
  1504. *
  1505. * @dev: Device that requests the nvmem cell.
  1506. * @cell_id: Name of nvmem cell to read.
  1507. * @val: pointer to output value.
  1508. *
  1509. * Return: 0 on success or negative errno.
  1510. */
  1511. int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
  1512. {
  1513. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1514. }
  1515. EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
  1516. /**
  1517. * nvmem_cell_read_u16() - Read a cell value as a u16
  1518. *
  1519. * @dev: Device that requests the nvmem cell.
  1520. * @cell_id: Name of nvmem cell to read.
  1521. * @val: pointer to output value.
  1522. *
  1523. * Return: 0 on success or negative errno.
  1524. */
  1525. int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
  1526. {
  1527. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1528. }
  1529. EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
  1530. /**
  1531. * nvmem_cell_read_u32() - Read a cell value as a u32
  1532. *
  1533. * @dev: Device that requests the nvmem cell.
  1534. * @cell_id: Name of nvmem cell to read.
  1535. * @val: pointer to output value.
  1536. *
  1537. * Return: 0 on success or negative errno.
  1538. */
  1539. int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
  1540. {
  1541. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1542. }
  1543. EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
  1544. /**
  1545. * nvmem_cell_read_u64() - Read a cell value as a u64
  1546. *
  1547. * @dev: Device that requests the nvmem cell.
  1548. * @cell_id: Name of nvmem cell to read.
  1549. * @val: pointer to output value.
  1550. *
  1551. * Return: 0 on success or negative errno.
  1552. */
  1553. int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
  1554. {
  1555. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1556. }
  1557. EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
  1558. static const void *nvmem_cell_read_variable_common(struct device *dev,
  1559. const char *cell_id,
  1560. size_t max_len, size_t *len)
  1561. {
  1562. struct nvmem_cell *cell;
  1563. int nbits;
  1564. void *buf;
  1565. cell = nvmem_cell_get(dev, cell_id);
  1566. if (IS_ERR(cell))
  1567. return cell;
  1568. nbits = cell->entry->nbits;
  1569. buf = nvmem_cell_read(cell, len);
  1570. nvmem_cell_put(cell);
  1571. if (IS_ERR(buf))
  1572. return buf;
  1573. /*
  1574. * If nbits is set then nvmem_cell_read() can significantly exaggerate
  1575. * the length of the real data. Throw away the extra junk.
  1576. */
  1577. if (nbits)
  1578. *len = DIV_ROUND_UP(nbits, 8);
  1579. if (*len > max_len) {
  1580. kfree(buf);
  1581. return ERR_PTR(-ERANGE);
  1582. }
  1583. return buf;
  1584. }
  1585. /**
  1586. * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
  1587. *
  1588. * @dev: Device that requests the nvmem cell.
  1589. * @cell_id: Name of nvmem cell to read.
  1590. * @val: pointer to output value.
  1591. *
  1592. * Return: 0 on success or negative errno.
  1593. */
  1594. int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
  1595. u32 *val)
  1596. {
  1597. size_t len;
  1598. const u8 *buf;
  1599. int i;
  1600. buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
  1601. if (IS_ERR(buf))
  1602. return PTR_ERR(buf);
  1603. /* Copy w/ implicit endian conversion */
  1604. *val = 0;
  1605. for (i = 0; i < len; i++)
  1606. *val |= buf[i] << (8 * i);
  1607. kfree(buf);
  1608. return 0;
  1609. }
  1610. EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
  1611. /**
  1612. * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
  1613. *
  1614. * @dev: Device that requests the nvmem cell.
  1615. * @cell_id: Name of nvmem cell to read.
  1616. * @val: pointer to output value.
  1617. *
  1618. * Return: 0 on success or negative errno.
  1619. */
  1620. int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
  1621. u64 *val)
  1622. {
  1623. size_t len;
  1624. const u8 *buf;
  1625. int i;
  1626. buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
  1627. if (IS_ERR(buf))
  1628. return PTR_ERR(buf);
  1629. /* Copy w/ implicit endian conversion */
  1630. *val = 0;
  1631. for (i = 0; i < len; i++)
  1632. *val |= (uint64_t)buf[i] << (8 * i);
  1633. kfree(buf);
  1634. return 0;
  1635. }
  1636. EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
  1637. /**
  1638. * nvmem_device_cell_read() - Read a given nvmem device and cell
  1639. *
  1640. * @nvmem: nvmem device to read from.
  1641. * @info: nvmem cell info to be read.
  1642. * @buf: buffer pointer which will be populated on successful read.
  1643. *
  1644. * Return: length of successful bytes read on success and negative
  1645. * error code on error.
  1646. */
  1647. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  1648. struct nvmem_cell_info *info, void *buf)
  1649. {
  1650. struct nvmem_cell_entry cell;
  1651. int rc;
  1652. ssize_t len;
  1653. if (!nvmem)
  1654. return -EINVAL;
  1655. rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
  1656. if (rc)
  1657. return rc;
  1658. rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
  1659. if (rc)
  1660. return rc;
  1661. return len;
  1662. }
  1663. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  1664. /**
  1665. * nvmem_device_cell_write() - Write cell to a given nvmem device
  1666. *
  1667. * @nvmem: nvmem device to be written to.
  1668. * @info: nvmem cell info to be written.
  1669. * @buf: buffer to be written to cell.
  1670. *
  1671. * Return: length of bytes written or negative error code on failure.
  1672. */
  1673. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  1674. struct nvmem_cell_info *info, void *buf)
  1675. {
  1676. struct nvmem_cell_entry cell;
  1677. int rc;
  1678. if (!nvmem)
  1679. return -EINVAL;
  1680. rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
  1681. if (rc)
  1682. return rc;
  1683. return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
  1684. }
  1685. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  1686. /**
  1687. * nvmem_device_read() - Read from a given nvmem device
  1688. *
  1689. * @nvmem: nvmem device to read from.
  1690. * @offset: offset in nvmem device.
  1691. * @bytes: number of bytes to read.
  1692. * @buf: buffer pointer which will be populated on successful read.
  1693. *
  1694. * Return: length of successful bytes read on success and negative
  1695. * error code on error.
  1696. */
  1697. int nvmem_device_read(struct nvmem_device *nvmem,
  1698. unsigned int offset,
  1699. size_t bytes, void *buf)
  1700. {
  1701. int rc;
  1702. if (!nvmem)
  1703. return -EINVAL;
  1704. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  1705. if (rc)
  1706. return rc;
  1707. return bytes;
  1708. }
  1709. EXPORT_SYMBOL_GPL(nvmem_device_read);
  1710. /**
  1711. * nvmem_device_write() - Write cell to a given nvmem device
  1712. *
  1713. * @nvmem: nvmem device to be written to.
  1714. * @offset: offset in nvmem device.
  1715. * @bytes: number of bytes to write.
  1716. * @buf: buffer to be written.
  1717. *
  1718. * Return: length of bytes written or negative error code on failure.
  1719. */
  1720. int nvmem_device_write(struct nvmem_device *nvmem,
  1721. unsigned int offset,
  1722. size_t bytes, void *buf)
  1723. {
  1724. int rc;
  1725. if (!nvmem)
  1726. return -EINVAL;
  1727. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  1728. if (rc)
  1729. return rc;
  1730. return bytes;
  1731. }
  1732. EXPORT_SYMBOL_GPL(nvmem_device_write);
  1733. /**
  1734. * nvmem_add_cell_lookups() - register a list of cell lookup entries
  1735. *
  1736. * @entries: array of cell lookup entries
  1737. * @nentries: number of cell lookup entries in the array
  1738. */
  1739. void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
  1740. {
  1741. int i;
  1742. mutex_lock(&nvmem_lookup_mutex);
  1743. for (i = 0; i < nentries; i++)
  1744. list_add_tail(&entries[i].node, &nvmem_lookup_list);
  1745. mutex_unlock(&nvmem_lookup_mutex);
  1746. }
  1747. EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
  1748. /**
  1749. * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
  1750. * entries
  1751. *
  1752. * @entries: array of cell lookup entries
  1753. * @nentries: number of cell lookup entries in the array
  1754. */
  1755. void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
  1756. {
  1757. int i;
  1758. mutex_lock(&nvmem_lookup_mutex);
  1759. for (i = 0; i < nentries; i++)
  1760. list_del(&entries[i].node);
  1761. mutex_unlock(&nvmem_lookup_mutex);
  1762. }
  1763. EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
  1764. /**
  1765. * nvmem_dev_name() - Get the name of a given nvmem device.
  1766. *
  1767. * @nvmem: nvmem device.
  1768. *
  1769. * Return: name of the nvmem device.
  1770. */
  1771. const char *nvmem_dev_name(struct nvmem_device *nvmem)
  1772. {
  1773. return dev_name(&nvmem->dev);
  1774. }
  1775. EXPORT_SYMBOL_GPL(nvmem_dev_name);
  1776. /**
  1777. * nvmem_dev_size() - Get the size of a given nvmem device.
  1778. *
  1779. * @nvmem: nvmem device.
  1780. *
  1781. * Return: size of the nvmem device.
  1782. */
  1783. size_t nvmem_dev_size(struct nvmem_device *nvmem)
  1784. {
  1785. return nvmem->size;
  1786. }
  1787. EXPORT_SYMBOL_GPL(nvmem_dev_size);
  1788. static int __init nvmem_init(void)
  1789. {
  1790. int ret;
  1791. ret = bus_register(&nvmem_bus_type);
  1792. if (ret)
  1793. return ret;
  1794. ret = nvmem_layout_bus_register();
  1795. if (ret)
  1796. bus_unregister(&nvmem_bus_type);
  1797. return ret;
  1798. }
  1799. static void __exit nvmem_exit(void)
  1800. {
  1801. nvmem_layout_bus_unregister();
  1802. bus_unregister(&nvmem_bus_type);
  1803. }
  1804. subsys_initcall(nvmem_init);
  1805. module_exit(nvmem_exit);
  1806. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
  1807. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  1808. MODULE_DESCRIPTION("nvmem Driver Core");