core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /*
  2. * nvmem framework core.
  3. *
  4. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  5. * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/export.h>
  18. #include <linux/fs.h>
  19. #include <linux/idr.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/nvmem-consumer.h>
  23. #include <linux/nvmem-provider.h>
  24. #include <linux/of.h>
  25. #include <linux/slab.h>
  26. struct nvmem_device {
  27. const char *name;
  28. struct module *owner;
  29. struct device dev;
  30. int stride;
  31. int word_size;
  32. int ncells;
  33. int id;
  34. int users;
  35. size_t size;
  36. bool read_only;
  37. int flags;
  38. struct bin_attribute eeprom;
  39. struct device *base_dev;
  40. nvmem_reg_read_t reg_read;
  41. nvmem_reg_write_t reg_write;
  42. void *priv;
  43. };
  44. #define FLAG_COMPAT BIT(0)
  45. struct nvmem_cell {
  46. const char *name;
  47. int offset;
  48. int bytes;
  49. int bit_offset;
  50. int nbits;
  51. struct nvmem_device *nvmem;
  52. struct list_head node;
  53. };
  54. static DEFINE_MUTEX(nvmem_mutex);
  55. static DEFINE_IDA(nvmem_ida);
  56. static LIST_HEAD(nvmem_cells);
  57. static DEFINE_MUTEX(nvmem_cells_mutex);
  58. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  59. static struct lock_class_key eeprom_lock_key;
  60. #endif
  61. #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  62. static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  63. void *val, size_t bytes)
  64. {
  65. if (nvmem->reg_read)
  66. return nvmem->reg_read(nvmem->priv, offset, val, bytes);
  67. return -EINVAL;
  68. }
  69. static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  70. void *val, size_t bytes)
  71. {
  72. if (nvmem->reg_write)
  73. return nvmem->reg_write(nvmem->priv, offset, val, bytes);
  74. return -EINVAL;
  75. }
  76. static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
  77. struct bin_attribute *attr,
  78. char *buf, loff_t pos, size_t count)
  79. {
  80. struct device *dev;
  81. struct nvmem_device *nvmem;
  82. int rc;
  83. if (attr->private)
  84. dev = attr->private;
  85. else
  86. dev = container_of(kobj, struct device, kobj);
  87. nvmem = to_nvmem_device(dev);
  88. /* Stop the user from reading */
  89. if (pos >= nvmem->size)
  90. return 0;
  91. if (count < nvmem->word_size)
  92. return -EINVAL;
  93. if (pos + count > nvmem->size)
  94. count = nvmem->size - pos;
  95. count = round_down(count, nvmem->word_size);
  96. rc = nvmem_reg_read(nvmem, pos, buf, count);
  97. if (rc)
  98. return rc;
  99. return count;
  100. }
  101. static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
  102. struct bin_attribute *attr,
  103. char *buf, loff_t pos, size_t count)
  104. {
  105. struct device *dev;
  106. struct nvmem_device *nvmem;
  107. int rc;
  108. if (attr->private)
  109. dev = attr->private;
  110. else
  111. dev = container_of(kobj, struct device, kobj);
  112. nvmem = to_nvmem_device(dev);
  113. /* Stop the user from writing */
  114. if (pos >= nvmem->size)
  115. return 0;
  116. if (count < nvmem->word_size)
  117. return -EINVAL;
  118. if (pos + count > nvmem->size)
  119. count = nvmem->size - pos;
  120. count = round_down(count, nvmem->word_size);
  121. rc = nvmem_reg_write(nvmem, pos, buf, count);
  122. if (rc)
  123. return rc;
  124. return count;
  125. }
  126. /* default read/write permissions */
  127. static struct bin_attribute bin_attr_rw_nvmem = {
  128. .attr = {
  129. .name = "nvmem",
  130. .mode = S_IWUSR | S_IRUGO,
  131. },
  132. .read = bin_attr_nvmem_read,
  133. .write = bin_attr_nvmem_write,
  134. };
  135. static struct bin_attribute *nvmem_bin_rw_attributes[] = {
  136. &bin_attr_rw_nvmem,
  137. NULL,
  138. };
  139. static const struct attribute_group nvmem_bin_rw_group = {
  140. .bin_attrs = nvmem_bin_rw_attributes,
  141. };
  142. static const struct attribute_group *nvmem_rw_dev_groups[] = {
  143. &nvmem_bin_rw_group,
  144. NULL,
  145. };
  146. /* read only permission */
  147. static struct bin_attribute bin_attr_ro_nvmem = {
  148. .attr = {
  149. .name = "nvmem",
  150. .mode = S_IRUGO,
  151. },
  152. .read = bin_attr_nvmem_read,
  153. };
  154. static struct bin_attribute *nvmem_bin_ro_attributes[] = {
  155. &bin_attr_ro_nvmem,
  156. NULL,
  157. };
  158. static const struct attribute_group nvmem_bin_ro_group = {
  159. .bin_attrs = nvmem_bin_ro_attributes,
  160. };
  161. static const struct attribute_group *nvmem_ro_dev_groups[] = {
  162. &nvmem_bin_ro_group,
  163. NULL,
  164. };
  165. /* default read/write permissions, root only */
  166. static struct bin_attribute bin_attr_rw_root_nvmem = {
  167. .attr = {
  168. .name = "nvmem",
  169. .mode = S_IWUSR | S_IRUSR,
  170. },
  171. .read = bin_attr_nvmem_read,
  172. .write = bin_attr_nvmem_write,
  173. };
  174. static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
  175. &bin_attr_rw_root_nvmem,
  176. NULL,
  177. };
  178. static const struct attribute_group nvmem_bin_rw_root_group = {
  179. .bin_attrs = nvmem_bin_rw_root_attributes,
  180. };
  181. static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
  182. &nvmem_bin_rw_root_group,
  183. NULL,
  184. };
  185. /* read only permission, root only */
  186. static struct bin_attribute bin_attr_ro_root_nvmem = {
  187. .attr = {
  188. .name = "nvmem",
  189. .mode = S_IRUSR,
  190. },
  191. .read = bin_attr_nvmem_read,
  192. };
  193. static struct bin_attribute *nvmem_bin_ro_root_attributes[] = {
  194. &bin_attr_ro_root_nvmem,
  195. NULL,
  196. };
  197. static const struct attribute_group nvmem_bin_ro_root_group = {
  198. .bin_attrs = nvmem_bin_ro_root_attributes,
  199. };
  200. static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
  201. &nvmem_bin_ro_root_group,
  202. NULL,
  203. };
  204. static void nvmem_release(struct device *dev)
  205. {
  206. struct nvmem_device *nvmem = to_nvmem_device(dev);
  207. ida_simple_remove(&nvmem_ida, nvmem->id);
  208. kfree(nvmem);
  209. }
  210. static const struct device_type nvmem_provider_type = {
  211. .release = nvmem_release,
  212. };
  213. static struct bus_type nvmem_bus_type = {
  214. .name = "nvmem",
  215. };
  216. static int of_nvmem_match(struct device *dev, void *nvmem_np)
  217. {
  218. return dev->of_node == nvmem_np;
  219. }
  220. static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
  221. {
  222. struct device *d;
  223. if (!nvmem_np)
  224. return NULL;
  225. d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
  226. if (!d)
  227. return NULL;
  228. return to_nvmem_device(d);
  229. }
  230. static struct nvmem_cell *nvmem_find_cell(const char *cell_id)
  231. {
  232. struct nvmem_cell *p;
  233. list_for_each_entry(p, &nvmem_cells, node)
  234. if (p && !strcmp(p->name, cell_id))
  235. return p;
  236. return NULL;
  237. }
  238. static void nvmem_cell_drop(struct nvmem_cell *cell)
  239. {
  240. mutex_lock(&nvmem_cells_mutex);
  241. list_del(&cell->node);
  242. mutex_unlock(&nvmem_cells_mutex);
  243. kfree(cell);
  244. }
  245. static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
  246. {
  247. struct nvmem_cell *cell;
  248. struct list_head *p, *n;
  249. list_for_each_safe(p, n, &nvmem_cells) {
  250. cell = list_entry(p, struct nvmem_cell, node);
  251. if (cell->nvmem == nvmem)
  252. nvmem_cell_drop(cell);
  253. }
  254. }
  255. static void nvmem_cell_add(struct nvmem_cell *cell)
  256. {
  257. mutex_lock(&nvmem_cells_mutex);
  258. list_add_tail(&cell->node, &nvmem_cells);
  259. mutex_unlock(&nvmem_cells_mutex);
  260. }
  261. static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
  262. const struct nvmem_cell_info *info,
  263. struct nvmem_cell *cell)
  264. {
  265. cell->nvmem = nvmem;
  266. cell->offset = info->offset;
  267. cell->bytes = info->bytes;
  268. cell->name = info->name;
  269. cell->bit_offset = info->bit_offset;
  270. cell->nbits = info->nbits;
  271. if (cell->nbits)
  272. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  273. BITS_PER_BYTE);
  274. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  275. dev_err(&nvmem->dev,
  276. "cell %s unaligned to nvmem stride %d\n",
  277. cell->name, nvmem->stride);
  278. return -EINVAL;
  279. }
  280. return 0;
  281. }
  282. static int nvmem_add_cells(struct nvmem_device *nvmem,
  283. const struct nvmem_config *cfg)
  284. {
  285. struct nvmem_cell **cells;
  286. const struct nvmem_cell_info *info = cfg->cells;
  287. int i, rval;
  288. cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL);
  289. if (!cells)
  290. return -ENOMEM;
  291. for (i = 0; i < cfg->ncells; i++) {
  292. cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
  293. if (!cells[i]) {
  294. rval = -ENOMEM;
  295. goto err;
  296. }
  297. rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
  298. if (rval) {
  299. kfree(cells[i]);
  300. goto err;
  301. }
  302. nvmem_cell_add(cells[i]);
  303. }
  304. nvmem->ncells = cfg->ncells;
  305. /* remove tmp array */
  306. kfree(cells);
  307. return 0;
  308. err:
  309. while (i--)
  310. nvmem_cell_drop(cells[i]);
  311. kfree(cells);
  312. return rval;
  313. }
  314. /*
  315. * nvmem_setup_compat() - Create an additional binary entry in
  316. * drivers sys directory, to be backwards compatible with the older
  317. * drivers/misc/eeprom drivers.
  318. */
  319. static int nvmem_setup_compat(struct nvmem_device *nvmem,
  320. const struct nvmem_config *config)
  321. {
  322. int rval;
  323. if (!config->base_dev)
  324. return -EINVAL;
  325. if (nvmem->read_only)
  326. nvmem->eeprom = bin_attr_ro_root_nvmem;
  327. else
  328. nvmem->eeprom = bin_attr_rw_root_nvmem;
  329. nvmem->eeprom.attr.name = "eeprom";
  330. nvmem->eeprom.size = nvmem->size;
  331. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  332. nvmem->eeprom.attr.key = &eeprom_lock_key;
  333. #endif
  334. nvmem->eeprom.private = &nvmem->dev;
  335. nvmem->base_dev = config->base_dev;
  336. rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
  337. if (rval) {
  338. dev_err(&nvmem->dev,
  339. "Failed to create eeprom binary file %d\n", rval);
  340. return rval;
  341. }
  342. nvmem->flags |= FLAG_COMPAT;
  343. return 0;
  344. }
  345. /**
  346. * nvmem_register() - Register a nvmem device for given nvmem_config.
  347. * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  348. *
  349. * @config: nvmem device configuration with which nvmem device is created.
  350. *
  351. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  352. * on success.
  353. */
  354. struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  355. {
  356. struct nvmem_device *nvmem;
  357. struct device_node *np;
  358. int rval;
  359. if (!config->dev)
  360. return ERR_PTR(-EINVAL);
  361. nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
  362. if (!nvmem)
  363. return ERR_PTR(-ENOMEM);
  364. rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
  365. if (rval < 0) {
  366. kfree(nvmem);
  367. return ERR_PTR(rval);
  368. }
  369. nvmem->id = rval;
  370. nvmem->owner = config->owner;
  371. nvmem->stride = config->stride;
  372. nvmem->word_size = config->word_size;
  373. nvmem->size = config->size;
  374. nvmem->dev.type = &nvmem_provider_type;
  375. nvmem->dev.bus = &nvmem_bus_type;
  376. nvmem->dev.parent = config->dev;
  377. nvmem->priv = config->priv;
  378. nvmem->reg_read = config->reg_read;
  379. nvmem->reg_write = config->reg_write;
  380. np = config->dev->of_node;
  381. nvmem->dev.of_node = np;
  382. dev_set_name(&nvmem->dev, "%s%d",
  383. config->name ? : "nvmem", config->id);
  384. nvmem->read_only = of_property_read_bool(np, "read-only") |
  385. config->read_only;
  386. if (config->root_only)
  387. nvmem->dev.groups = nvmem->read_only ?
  388. nvmem_ro_root_dev_groups :
  389. nvmem_rw_root_dev_groups;
  390. else
  391. nvmem->dev.groups = nvmem->read_only ?
  392. nvmem_ro_dev_groups :
  393. nvmem_rw_dev_groups;
  394. device_initialize(&nvmem->dev);
  395. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  396. rval = device_add(&nvmem->dev);
  397. if (rval)
  398. goto err_put_device;
  399. if (config->compat) {
  400. rval = nvmem_setup_compat(nvmem, config);
  401. if (rval)
  402. goto err_device_del;
  403. }
  404. if (config->cells)
  405. nvmem_add_cells(nvmem, config);
  406. return nvmem;
  407. err_device_del:
  408. device_del(&nvmem->dev);
  409. err_put_device:
  410. put_device(&nvmem->dev);
  411. return ERR_PTR(rval);
  412. }
  413. EXPORT_SYMBOL_GPL(nvmem_register);
  414. /**
  415. * nvmem_unregister() - Unregister previously registered nvmem device
  416. *
  417. * @nvmem: Pointer to previously registered nvmem device.
  418. *
  419. * Return: Will be an negative on error or a zero on success.
  420. */
  421. int nvmem_unregister(struct nvmem_device *nvmem)
  422. {
  423. mutex_lock(&nvmem_mutex);
  424. if (nvmem->users) {
  425. mutex_unlock(&nvmem_mutex);
  426. return -EBUSY;
  427. }
  428. mutex_unlock(&nvmem_mutex);
  429. if (nvmem->flags & FLAG_COMPAT)
  430. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  431. nvmem_device_remove_all_cells(nvmem);
  432. device_del(&nvmem->dev);
  433. return 0;
  434. }
  435. EXPORT_SYMBOL_GPL(nvmem_unregister);
  436. static struct nvmem_device *__nvmem_device_get(struct device_node *np,
  437. struct nvmem_cell **cellp,
  438. const char *cell_id)
  439. {
  440. struct nvmem_device *nvmem = NULL;
  441. mutex_lock(&nvmem_mutex);
  442. if (np) {
  443. nvmem = of_nvmem_find(np);
  444. if (!nvmem) {
  445. mutex_unlock(&nvmem_mutex);
  446. return ERR_PTR(-EPROBE_DEFER);
  447. }
  448. } else {
  449. struct nvmem_cell *cell = nvmem_find_cell(cell_id);
  450. if (cell) {
  451. nvmem = cell->nvmem;
  452. *cellp = cell;
  453. }
  454. if (!nvmem) {
  455. mutex_unlock(&nvmem_mutex);
  456. return ERR_PTR(-ENOENT);
  457. }
  458. }
  459. nvmem->users++;
  460. mutex_unlock(&nvmem_mutex);
  461. if (!try_module_get(nvmem->owner)) {
  462. dev_err(&nvmem->dev,
  463. "could not increase module refcount for cell %s\n",
  464. nvmem->name);
  465. mutex_lock(&nvmem_mutex);
  466. nvmem->users--;
  467. mutex_unlock(&nvmem_mutex);
  468. return ERR_PTR(-EINVAL);
  469. }
  470. return nvmem;
  471. }
  472. static void __nvmem_device_put(struct nvmem_device *nvmem)
  473. {
  474. module_put(nvmem->owner);
  475. mutex_lock(&nvmem_mutex);
  476. nvmem->users--;
  477. mutex_unlock(&nvmem_mutex);
  478. }
  479. static int nvmem_match(struct device *dev, void *data)
  480. {
  481. return !strcmp(dev_name(dev), data);
  482. }
  483. static struct nvmem_device *nvmem_find(const char *name)
  484. {
  485. struct device *d;
  486. d = bus_find_device(&nvmem_bus_type, NULL, (void *)name, nvmem_match);
  487. if (!d)
  488. return NULL;
  489. return to_nvmem_device(d);
  490. }
  491. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  492. /**
  493. * of_nvmem_device_get() - Get nvmem device from a given id
  494. *
  495. * @dev node: Device tree node that uses the nvmem device
  496. * @id: nvmem name from nvmem-names property.
  497. *
  498. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  499. * on success.
  500. */
  501. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  502. {
  503. struct device_node *nvmem_np;
  504. int index;
  505. index = of_property_match_string(np, "nvmem-names", id);
  506. nvmem_np = of_parse_phandle(np, "nvmem", index);
  507. if (!nvmem_np)
  508. return ERR_PTR(-EINVAL);
  509. return __nvmem_device_get(nvmem_np, NULL, NULL);
  510. }
  511. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  512. #endif
  513. /**
  514. * nvmem_device_get() - Get nvmem device from a given id
  515. *
  516. * @dev : Device that uses the nvmem device
  517. * @id: nvmem name from nvmem-names property.
  518. *
  519. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  520. * on success.
  521. */
  522. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  523. {
  524. if (dev->of_node) { /* try dt first */
  525. struct nvmem_device *nvmem;
  526. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  527. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  528. return nvmem;
  529. }
  530. return nvmem_find(dev_name);
  531. }
  532. EXPORT_SYMBOL_GPL(nvmem_device_get);
  533. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  534. {
  535. struct nvmem_device **nvmem = res;
  536. if (WARN_ON(!nvmem || !*nvmem))
  537. return 0;
  538. return *nvmem == data;
  539. }
  540. static void devm_nvmem_device_release(struct device *dev, void *res)
  541. {
  542. nvmem_device_put(*(struct nvmem_device **)res);
  543. }
  544. /**
  545. * devm_nvmem_device_put() - put alredy got nvmem device
  546. *
  547. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  548. * that needs to be released.
  549. */
  550. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  551. {
  552. int ret;
  553. ret = devres_release(dev, devm_nvmem_device_release,
  554. devm_nvmem_device_match, nvmem);
  555. WARN_ON(ret);
  556. }
  557. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  558. /**
  559. * nvmem_device_put() - put alredy got nvmem device
  560. *
  561. * @nvmem: pointer to nvmem device that needs to be released.
  562. */
  563. void nvmem_device_put(struct nvmem_device *nvmem)
  564. {
  565. __nvmem_device_put(nvmem);
  566. }
  567. EXPORT_SYMBOL_GPL(nvmem_device_put);
  568. /**
  569. * devm_nvmem_device_get() - Get nvmem cell of device form a given id
  570. *
  571. * @dev node: Device tree node that uses the nvmem cell
  572. * @id: nvmem name in nvmems property.
  573. *
  574. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
  575. * on success. The nvmem_cell will be freed by the automatically once the
  576. * device is freed.
  577. */
  578. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  579. {
  580. struct nvmem_device **ptr, *nvmem;
  581. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  582. if (!ptr)
  583. return ERR_PTR(-ENOMEM);
  584. nvmem = nvmem_device_get(dev, id);
  585. if (!IS_ERR(nvmem)) {
  586. *ptr = nvmem;
  587. devres_add(dev, ptr);
  588. } else {
  589. devres_free(ptr);
  590. }
  591. return nvmem;
  592. }
  593. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  594. static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id)
  595. {
  596. struct nvmem_cell *cell = NULL;
  597. struct nvmem_device *nvmem;
  598. nvmem = __nvmem_device_get(NULL, &cell, cell_id);
  599. if (IS_ERR(nvmem))
  600. return ERR_CAST(nvmem);
  601. return cell;
  602. }
  603. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  604. /**
  605. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  606. *
  607. * @dev node: Device tree node that uses the nvmem cell
  608. * @id: nvmem cell name from nvmem-cell-names property.
  609. *
  610. * Return: Will be an ERR_PTR() on error or a valid pointer
  611. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  612. * nvmem_cell_put().
  613. */
  614. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
  615. const char *name)
  616. {
  617. struct device_node *cell_np, *nvmem_np;
  618. struct nvmem_cell *cell;
  619. struct nvmem_device *nvmem;
  620. const __be32 *addr;
  621. int rval, len, index;
  622. index = of_property_match_string(np, "nvmem-cell-names", name);
  623. cell_np = of_parse_phandle(np, "nvmem-cells", index);
  624. if (!cell_np)
  625. return ERR_PTR(-EINVAL);
  626. nvmem_np = of_get_next_parent(cell_np);
  627. if (!nvmem_np)
  628. return ERR_PTR(-EINVAL);
  629. nvmem = __nvmem_device_get(nvmem_np, NULL, NULL);
  630. if (IS_ERR(nvmem))
  631. return ERR_CAST(nvmem);
  632. addr = of_get_property(cell_np, "reg", &len);
  633. if (!addr || (len < 2 * sizeof(u32))) {
  634. dev_err(&nvmem->dev, "nvmem: invalid reg on %s\n",
  635. cell_np->full_name);
  636. rval = -EINVAL;
  637. goto err_mem;
  638. }
  639. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  640. if (!cell) {
  641. rval = -ENOMEM;
  642. goto err_mem;
  643. }
  644. cell->nvmem = nvmem;
  645. cell->offset = be32_to_cpup(addr++);
  646. cell->bytes = be32_to_cpup(addr);
  647. cell->name = cell_np->name;
  648. addr = of_get_property(cell_np, "bits", &len);
  649. if (addr && len == (2 * sizeof(u32))) {
  650. cell->bit_offset = be32_to_cpup(addr++);
  651. cell->nbits = be32_to_cpup(addr);
  652. }
  653. if (cell->nbits)
  654. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  655. BITS_PER_BYTE);
  656. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  657. dev_err(&nvmem->dev,
  658. "cell %s unaligned to nvmem stride %d\n",
  659. cell->name, nvmem->stride);
  660. rval = -EINVAL;
  661. goto err_sanity;
  662. }
  663. nvmem_cell_add(cell);
  664. return cell;
  665. err_sanity:
  666. kfree(cell);
  667. err_mem:
  668. __nvmem_device_put(nvmem);
  669. return ERR_PTR(rval);
  670. }
  671. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  672. #endif
  673. /**
  674. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  675. *
  676. * @dev node: Device tree node that uses the nvmem cell
  677. * @id: nvmem cell name to get.
  678. *
  679. * Return: Will be an ERR_PTR() on error or a valid pointer
  680. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  681. * nvmem_cell_put().
  682. */
  683. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id)
  684. {
  685. struct nvmem_cell *cell;
  686. if (dev->of_node) { /* try dt first */
  687. cell = of_nvmem_cell_get(dev->of_node, cell_id);
  688. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  689. return cell;
  690. }
  691. return nvmem_cell_get_from_list(cell_id);
  692. }
  693. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  694. static void devm_nvmem_cell_release(struct device *dev, void *res)
  695. {
  696. nvmem_cell_put(*(struct nvmem_cell **)res);
  697. }
  698. /**
  699. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  700. *
  701. * @dev node: Device tree node that uses the nvmem cell
  702. * @id: nvmem id in nvmem-names property.
  703. *
  704. * Return: Will be an ERR_PTR() on error or a valid pointer
  705. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  706. * automatically once the device is freed.
  707. */
  708. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  709. {
  710. struct nvmem_cell **ptr, *cell;
  711. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  712. if (!ptr)
  713. return ERR_PTR(-ENOMEM);
  714. cell = nvmem_cell_get(dev, id);
  715. if (!IS_ERR(cell)) {
  716. *ptr = cell;
  717. devres_add(dev, ptr);
  718. } else {
  719. devres_free(ptr);
  720. }
  721. return cell;
  722. }
  723. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  724. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  725. {
  726. struct nvmem_cell **c = res;
  727. if (WARN_ON(!c || !*c))
  728. return 0;
  729. return *c == data;
  730. }
  731. /**
  732. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  733. * from devm_nvmem_cell_get.
  734. *
  735. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get()
  736. */
  737. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  738. {
  739. int ret;
  740. ret = devres_release(dev, devm_nvmem_cell_release,
  741. devm_nvmem_cell_match, cell);
  742. WARN_ON(ret);
  743. }
  744. EXPORT_SYMBOL(devm_nvmem_cell_put);
  745. /**
  746. * nvmem_cell_put() - Release previously allocated nvmem cell.
  747. *
  748. * @cell: Previously allocated nvmem cell by nvmem_cell_get()
  749. */
  750. void nvmem_cell_put(struct nvmem_cell *cell)
  751. {
  752. struct nvmem_device *nvmem = cell->nvmem;
  753. __nvmem_device_put(nvmem);
  754. nvmem_cell_drop(cell);
  755. }
  756. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  757. static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell,
  758. void *buf)
  759. {
  760. u8 *p, *b;
  761. int i, bit_offset = cell->bit_offset;
  762. p = b = buf;
  763. if (bit_offset) {
  764. /* First shift */
  765. *b++ >>= bit_offset;
  766. /* setup rest of the bytes if any */
  767. for (i = 1; i < cell->bytes; i++) {
  768. /* Get bits from next byte and shift them towards msb */
  769. *p |= *b << (BITS_PER_BYTE - bit_offset);
  770. p = b;
  771. *b++ >>= bit_offset;
  772. }
  773. /* result fits in less bytes */
  774. if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
  775. *p-- = 0;
  776. }
  777. /* clear msb bits if any leftover in the last byte */
  778. *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
  779. }
  780. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  781. struct nvmem_cell *cell,
  782. void *buf, size_t *len)
  783. {
  784. int rc;
  785. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
  786. if (rc)
  787. return rc;
  788. /* shift bits in-place */
  789. if (cell->bit_offset || cell->nbits)
  790. nvmem_shift_read_buffer_in_place(cell, buf);
  791. *len = cell->bytes;
  792. return 0;
  793. }
  794. /**
  795. * nvmem_cell_read() - Read a given nvmem cell
  796. *
  797. * @cell: nvmem cell to be read.
  798. * @len: pointer to length of cell which will be populated on successful read.
  799. *
  800. * Return: ERR_PTR() on error or a valid pointer to a char * buffer on success.
  801. * The buffer should be freed by the consumer with a kfree().
  802. */
  803. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  804. {
  805. struct nvmem_device *nvmem = cell->nvmem;
  806. u8 *buf;
  807. int rc;
  808. if (!nvmem)
  809. return ERR_PTR(-EINVAL);
  810. buf = kzalloc(cell->bytes, GFP_KERNEL);
  811. if (!buf)
  812. return ERR_PTR(-ENOMEM);
  813. rc = __nvmem_cell_read(nvmem, cell, buf, len);
  814. if (rc) {
  815. kfree(buf);
  816. return ERR_PTR(rc);
  817. }
  818. return buf;
  819. }
  820. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  821. static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
  822. u8 *_buf, int len)
  823. {
  824. struct nvmem_device *nvmem = cell->nvmem;
  825. int i, rc, nbits, bit_offset = cell->bit_offset;
  826. u8 v, *p, *buf, *b, pbyte, pbits;
  827. nbits = cell->nbits;
  828. buf = kzalloc(cell->bytes, GFP_KERNEL);
  829. if (!buf)
  830. return ERR_PTR(-ENOMEM);
  831. memcpy(buf, _buf, len);
  832. p = b = buf;
  833. if (bit_offset) {
  834. pbyte = *b;
  835. *b <<= bit_offset;
  836. /* setup the first byte with lsb bits from nvmem */
  837. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  838. if (rc)
  839. goto err;
  840. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  841. /* setup rest of the byte if any */
  842. for (i = 1; i < cell->bytes; i++) {
  843. /* Get last byte bits and shift them towards lsb */
  844. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  845. pbyte = *b;
  846. p = b;
  847. *b <<= bit_offset;
  848. *b++ |= pbits;
  849. }
  850. }
  851. /* if it's not end on byte boundary */
  852. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  853. /* setup the last byte with msb bits from nvmem */
  854. rc = nvmem_reg_read(nvmem,
  855. cell->offset + cell->bytes - 1, &v, 1);
  856. if (rc)
  857. goto err;
  858. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  859. }
  860. return buf;
  861. err:
  862. kfree(buf);
  863. return ERR_PTR(rc);
  864. }
  865. /**
  866. * nvmem_cell_write() - Write to a given nvmem cell
  867. *
  868. * @cell: nvmem cell to be written.
  869. * @buf: Buffer to be written.
  870. * @len: length of buffer to be written to nvmem cell.
  871. *
  872. * Return: length of bytes written or negative on failure.
  873. */
  874. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  875. {
  876. struct nvmem_device *nvmem = cell->nvmem;
  877. int rc;
  878. if (!nvmem || nvmem->read_only ||
  879. (cell->bit_offset == 0 && len != cell->bytes))
  880. return -EINVAL;
  881. if (cell->bit_offset || cell->nbits) {
  882. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  883. if (IS_ERR(buf))
  884. return PTR_ERR(buf);
  885. }
  886. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  887. /* free the tmp buffer */
  888. if (cell->bit_offset || cell->nbits)
  889. kfree(buf);
  890. if (rc)
  891. return rc;
  892. return len;
  893. }
  894. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  895. /**
  896. * nvmem_device_cell_read() - Read a given nvmem device and cell
  897. *
  898. * @nvmem: nvmem device to read from.
  899. * @info: nvmem cell info to be read.
  900. * @buf: buffer pointer which will be populated on successful read.
  901. *
  902. * Return: length of successful bytes read on success and negative
  903. * error code on error.
  904. */
  905. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  906. struct nvmem_cell_info *info, void *buf)
  907. {
  908. struct nvmem_cell cell;
  909. int rc;
  910. ssize_t len;
  911. if (!nvmem)
  912. return -EINVAL;
  913. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  914. if (rc)
  915. return rc;
  916. rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
  917. if (rc)
  918. return rc;
  919. return len;
  920. }
  921. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  922. /**
  923. * nvmem_device_cell_write() - Write cell to a given nvmem device
  924. *
  925. * @nvmem: nvmem device to be written to.
  926. * @info: nvmem cell info to be written
  927. * @buf: buffer to be written to cell.
  928. *
  929. * Return: length of bytes written or negative error code on failure.
  930. * */
  931. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  932. struct nvmem_cell_info *info, void *buf)
  933. {
  934. struct nvmem_cell cell;
  935. int rc;
  936. if (!nvmem)
  937. return -EINVAL;
  938. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  939. if (rc)
  940. return rc;
  941. return nvmem_cell_write(&cell, buf, cell.bytes);
  942. }
  943. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  944. /**
  945. * nvmem_device_read() - Read from a given nvmem device
  946. *
  947. * @nvmem: nvmem device to read from.
  948. * @offset: offset in nvmem device.
  949. * @bytes: number of bytes to read.
  950. * @buf: buffer pointer which will be populated on successful read.
  951. *
  952. * Return: length of successful bytes read on success and negative
  953. * error code on error.
  954. */
  955. int nvmem_device_read(struct nvmem_device *nvmem,
  956. unsigned int offset,
  957. size_t bytes, void *buf)
  958. {
  959. int rc;
  960. if (!nvmem)
  961. return -EINVAL;
  962. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  963. if (rc)
  964. return rc;
  965. return bytes;
  966. }
  967. EXPORT_SYMBOL_GPL(nvmem_device_read);
  968. /**
  969. * nvmem_device_write() - Write cell to a given nvmem device
  970. *
  971. * @nvmem: nvmem device to be written to.
  972. * @offset: offset in nvmem device.
  973. * @bytes: number of bytes to write.
  974. * @buf: buffer to be written.
  975. *
  976. * Return: length of bytes written or negative error code on failure.
  977. * */
  978. int nvmem_device_write(struct nvmem_device *nvmem,
  979. unsigned int offset,
  980. size_t bytes, void *buf)
  981. {
  982. int rc;
  983. if (!nvmem)
  984. return -EINVAL;
  985. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  986. if (rc)
  987. return rc;
  988. return bytes;
  989. }
  990. EXPORT_SYMBOL_GPL(nvmem_device_write);
  991. static int __init nvmem_init(void)
  992. {
  993. return bus_register(&nvmem_bus_type);
  994. }
  995. static void __exit nvmem_exit(void)
  996. {
  997. bus_unregister(&nvmem_bus_type);
  998. }
  999. subsys_initcall(nvmem_init);
  1000. module_exit(nvmem_exit);
  1001. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  1002. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  1003. MODULE_DESCRIPTION("nvmem Driver Core");
  1004. MODULE_LICENSE("GPL v2");