Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * tps65910.c  --  TI TPS6591x
0004  *
0005  * Copyright 2010 Texas Instruments Inc.
0006  *
0007  * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/slab.h>
0014 #include <linux/err.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/debugfs.h>
0017 #include <linux/gpio.h>
0018 #include <linux/mfd/tps65910.h>
0019 
0020 #define COMP1                   0
0021 #define COMP2                   1
0022 
0023 /* Comparator 1 voltage selection table in millivolts */
0024 static const u16 COMP_VSEL_TABLE[] = {
0025     0, 2500, 2500, 2500, 2500, 2550, 2600, 2650,
0026     2700, 2750, 2800, 2850, 2900, 2950, 3000, 3050,
0027     3100, 3150, 3200, 3250, 3300, 3350, 3400, 3450,
0028     3500,
0029 };
0030 
0031 struct comparator {
0032     const char *name;
0033     int reg;
0034     int uV_max;
0035     const u16 *vsel_table;
0036 };
0037 
0038 static struct comparator tps_comparators[] = {
0039     {
0040         .name = "COMP1",
0041         .reg = TPS65911_VMBCH,
0042         .uV_max = 3500,
0043         .vsel_table = COMP_VSEL_TABLE,
0044     },
0045     {
0046         .name = "COMP2",
0047         .reg = TPS65911_VMBCH2,
0048         .uV_max = 3500,
0049         .vsel_table = COMP_VSEL_TABLE,
0050     },
0051 };
0052 
0053 static int comp_threshold_set(struct tps65910 *tps65910, int id, int voltage)
0054 {
0055     struct comparator tps_comp = tps_comparators[id];
0056     int curr_voltage = 0;
0057     int ret;
0058     u8 index = 0, val;
0059 
0060     while (curr_voltage < tps_comp.uV_max) {
0061         curr_voltage = tps_comp.vsel_table[index];
0062         if (curr_voltage >= voltage)
0063             break;
0064         else if (curr_voltage < voltage)
0065             index ++;
0066     }
0067 
0068     if (curr_voltage > tps_comp.uV_max)
0069         return -EINVAL;
0070 
0071     val = index << 1;
0072     ret = regmap_write(tps65910->regmap, tps_comp.reg, val);
0073 
0074     return ret;
0075 }
0076 
0077 static int comp_threshold_get(struct tps65910 *tps65910, int id)
0078 {
0079     struct comparator tps_comp = tps_comparators[id];
0080     unsigned int val;
0081     int ret;
0082 
0083     ret = regmap_read(tps65910->regmap, tps_comp.reg, &val);
0084     if (ret < 0)
0085         return ret;
0086 
0087     val >>= 1;
0088     return tps_comp.vsel_table[val];
0089 }
0090 
0091 static ssize_t comp_threshold_show(struct device *dev,
0092                 struct device_attribute *attr, char *buf)
0093 {
0094     struct tps65910 *tps65910 = dev_get_drvdata(dev->parent);
0095     struct attribute comp_attr = attr->attr;
0096     int id, uVolt;
0097 
0098     if (!strcmp(comp_attr.name, "comp1_threshold"))
0099         id = COMP1;
0100     else if (!strcmp(comp_attr.name, "comp2_threshold"))
0101         id = COMP2;
0102     else
0103         return -EINVAL;
0104 
0105     uVolt = comp_threshold_get(tps65910, id);
0106 
0107     return sprintf(buf, "%d\n", uVolt);
0108 }
0109 
0110 static DEVICE_ATTR(comp1_threshold, S_IRUGO, comp_threshold_show, NULL);
0111 static DEVICE_ATTR(comp2_threshold, S_IRUGO, comp_threshold_show, NULL);
0112 
0113 static int tps65911_comparator_probe(struct platform_device *pdev)
0114 {
0115     struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
0116     struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
0117     int ret;
0118 
0119     ret = comp_threshold_set(tps65910, COMP1,  pdata->vmbch_threshold);
0120     if (ret < 0) {
0121         dev_err(&pdev->dev, "cannot set COMP1 threshold\n");
0122         return ret;
0123     }
0124 
0125     ret = comp_threshold_set(tps65910, COMP2, pdata->vmbch2_threshold);
0126     if (ret < 0) {
0127         dev_err(&pdev->dev, "cannot set COMP2 threshold\n");
0128         return ret;
0129     }
0130 
0131     /* Create sysfs entry */
0132     ret = device_create_file(&pdev->dev, &dev_attr_comp1_threshold);
0133     if (ret < 0)
0134         dev_err(&pdev->dev, "failed to add COMP1 sysfs file\n");
0135 
0136     ret = device_create_file(&pdev->dev, &dev_attr_comp2_threshold);
0137     if (ret < 0)
0138         dev_err(&pdev->dev, "failed to add COMP2 sysfs file\n");
0139 
0140     return ret;
0141 }
0142 
0143 static int tps65911_comparator_remove(struct platform_device *pdev)
0144 {
0145     struct tps65910 *tps65910;
0146 
0147     tps65910 = dev_get_drvdata(pdev->dev.parent);
0148     device_remove_file(&pdev->dev, &dev_attr_comp2_threshold);
0149     device_remove_file(&pdev->dev, &dev_attr_comp1_threshold);
0150 
0151     return 0;
0152 }
0153 
0154 static struct platform_driver tps65911_comparator_driver = {
0155     .driver = {
0156         .name = "tps65911-comparator",
0157     },
0158     .probe = tps65911_comparator_probe,
0159     .remove = tps65911_comparator_remove,
0160 };
0161 
0162 static int __init tps65911_comparator_init(void)
0163 {
0164     return platform_driver_register(&tps65911_comparator_driver);
0165 }
0166 subsys_initcall(tps65911_comparator_init);
0167 
0168 static void __exit tps65911_comparator_exit(void)
0169 {
0170     platform_driver_unregister(&tps65911_comparator_driver);
0171 }
0172 module_exit(tps65911_comparator_exit);
0173 
0174 MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
0175 MODULE_DESCRIPTION("TPS65911 comparator driver");
0176 MODULE_LICENSE("GPL v2");
0177 MODULE_ALIAS("platform:tps65911-comparator");