core.c 51 KB

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