1 /* 2 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 and 6 * only version 2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #include <linux/device.h> 15 #include <linux/module.h> 16 #include <linux/io.h> 17 #include <linux/nvmem-provider.h> 18 #include <linux/platform_device.h> 19 20 static int qfprom_reg_read(void *context, 21 unsigned int reg, void *_val, size_t bytes) 22 { 23 void __iomem *base = context; 24 u8 *val = _val; 25 int i = 0, words = bytes; 26 27 while (words--) 28 *val++ = readb(base + reg + i++); 29 30 return 0; 31 } 32 33 static int qfprom_reg_write(void *context, 34 unsigned int reg, void *_val, size_t bytes) 35 { 36 void __iomem *base = context; 37 u8 *val = _val; 38 int i = 0, words = bytes; 39 40 while (words--) 41 writeb(*val++, base + reg + i++); 42 43 return 0; 44 } 45 46 static int qfprom_remove(struct platform_device *pdev) 47 { 48 struct nvmem_device *nvmem = platform_get_drvdata(pdev); 49 50 return nvmem_unregister(nvmem); 51 } 52 53 static struct nvmem_config econfig = { 54 .name = "qfprom", 55 .owner = THIS_MODULE, 56 .stride = 1, 57 .word_size = 1, 58 .reg_read = qfprom_reg_read, 59 .reg_write = qfprom_reg_write, 60 }; 61 62 static int qfprom_probe(struct platform_device *pdev) 63 { 64 struct device *dev = &pdev->dev; 65 struct resource *res; 66 struct nvmem_device *nvmem; 67 void __iomem *base; 68 69 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 70 base = devm_ioremap_resource(dev, res); 71 if (IS_ERR(base)) 72 return PTR_ERR(base); 73 74 econfig.size = resource_size(res); 75 econfig.dev = dev; 76 econfig.priv = base; 77 78 nvmem = nvmem_register(&econfig); 79 if (IS_ERR(nvmem)) 80 return PTR_ERR(nvmem); 81 82 platform_set_drvdata(pdev, nvmem); 83 84 return 0; 85 } 86 87 static const struct of_device_id qfprom_of_match[] = { 88 { .compatible = "qcom,qfprom",}, 89 {/* sentinel */}, 90 }; 91 MODULE_DEVICE_TABLE(of, qfprom_of_match); 92 93 static struct platform_driver qfprom_driver = { 94 .probe = qfprom_probe, 95 .remove = qfprom_remove, 96 .driver = { 97 .name = "qcom,qfprom", 98 .of_match_table = qfprom_of_match, 99 }, 100 }; 101 module_platform_driver(qfprom_driver); 102 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>"); 103 MODULE_DESCRIPTION("Qualcomm QFPROM driver"); 104 MODULE_LICENSE("GPL v2"); 105