1*93ee0a75SLuotao Fu /* 2*93ee0a75SLuotao Fu * Driver for the Freescale Semiconductor MC13783 adc. 3*93ee0a75SLuotao Fu * 4*93ee0a75SLuotao Fu * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved. 5*93ee0a75SLuotao Fu * Copyright (C) 2009 Sascha Hauer, Pengutronix 6*93ee0a75SLuotao Fu * 7*93ee0a75SLuotao Fu * This program is free software; you can redistribute it and/or 8*93ee0a75SLuotao Fu * modify it under the terms of the GNU General Public License 9*93ee0a75SLuotao Fu * as published by the Free Software Foundation; either version 2 10*93ee0a75SLuotao Fu * of the License, or (at your option) any later version. 11*93ee0a75SLuotao Fu * This program is distributed in the hope that it will be useful, 12*93ee0a75SLuotao Fu * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*93ee0a75SLuotao Fu * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*93ee0a75SLuotao Fu * GNU General Public License for more details. 15*93ee0a75SLuotao Fu * 16*93ee0a75SLuotao Fu * You should have received a copy of the GNU General Public License along with 17*93ee0a75SLuotao Fu * this program; if not, write to the Free Software Foundation, Inc., 51 18*93ee0a75SLuotao Fu * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19*93ee0a75SLuotao Fu */ 20*93ee0a75SLuotao Fu 21*93ee0a75SLuotao Fu #include <linux/mfd/mc13783-private.h> 22*93ee0a75SLuotao Fu #include <linux/platform_device.h> 23*93ee0a75SLuotao Fu #include <linux/hwmon-sysfs.h> 24*93ee0a75SLuotao Fu #include <linux/kernel.h> 25*93ee0a75SLuotao Fu #include <linux/module.h> 26*93ee0a75SLuotao Fu #include <linux/hwmon.h> 27*93ee0a75SLuotao Fu #include <linux/init.h> 28*93ee0a75SLuotao Fu #include <linux/err.h> 29*93ee0a75SLuotao Fu 30*93ee0a75SLuotao Fu #define MC13783_ADC_NAME "mc13783-adc" 31*93ee0a75SLuotao Fu 32*93ee0a75SLuotao Fu struct mc13783_adc_priv { 33*93ee0a75SLuotao Fu struct mc13783 *mc13783; 34*93ee0a75SLuotao Fu struct device *hwmon_dev; 35*93ee0a75SLuotao Fu }; 36*93ee0a75SLuotao Fu 37*93ee0a75SLuotao Fu static ssize_t mc13783_adc_show_name(struct device *dev, struct device_attribute 38*93ee0a75SLuotao Fu *devattr, char *buf) 39*93ee0a75SLuotao Fu { 40*93ee0a75SLuotao Fu return sprintf(buf, "mc13783_adc\n"); 41*93ee0a75SLuotao Fu } 42*93ee0a75SLuotao Fu 43*93ee0a75SLuotao Fu static int mc13783_adc_read(struct device *dev, 44*93ee0a75SLuotao Fu struct device_attribute *devattr, unsigned int *val) 45*93ee0a75SLuotao Fu { 46*93ee0a75SLuotao Fu struct platform_device *pdev = to_platform_device(dev); 47*93ee0a75SLuotao Fu struct mc13783_adc_priv *priv = platform_get_drvdata(pdev); 48*93ee0a75SLuotao Fu struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 49*93ee0a75SLuotao Fu unsigned int channel = attr->index; 50*93ee0a75SLuotao Fu unsigned int sample[4]; 51*93ee0a75SLuotao Fu int ret; 52*93ee0a75SLuotao Fu 53*93ee0a75SLuotao Fu ret = mc13783_adc_do_conversion(priv->mc13783, 54*93ee0a75SLuotao Fu MC13783_ADC_MODE_MULT_CHAN, 55*93ee0a75SLuotao Fu channel, sample); 56*93ee0a75SLuotao Fu if (ret) 57*93ee0a75SLuotao Fu return ret; 58*93ee0a75SLuotao Fu 59*93ee0a75SLuotao Fu channel &= 0x7; 60*93ee0a75SLuotao Fu 61*93ee0a75SLuotao Fu *val = (sample[channel % 4] >> (channel > 3 ? 14 : 2)) & 0x3ff; 62*93ee0a75SLuotao Fu 63*93ee0a75SLuotao Fu return 0; 64*93ee0a75SLuotao Fu } 65*93ee0a75SLuotao Fu 66*93ee0a75SLuotao Fu static ssize_t mc13783_adc_read_bp(struct device *dev, 67*93ee0a75SLuotao Fu struct device_attribute *devattr, char *buf) 68*93ee0a75SLuotao Fu { 69*93ee0a75SLuotao Fu unsigned val; 70*93ee0a75SLuotao Fu int ret = mc13783_adc_read(dev, devattr, &val); 71*93ee0a75SLuotao Fu 72*93ee0a75SLuotao Fu if (ret) 73*93ee0a75SLuotao Fu return ret; 74*93ee0a75SLuotao Fu 75*93ee0a75SLuotao Fu /* 76*93ee0a75SLuotao Fu * BP (channel 2) reports with offset 2.4V to the actual value to fit 77*93ee0a75SLuotao Fu * the input range of the ADC. unit = 2.25mV = 9/4 mV. 78*93ee0a75SLuotao Fu */ 79*93ee0a75SLuotao Fu val = DIV_ROUND_CLOSEST(val * 9, 4) + 2400; 80*93ee0a75SLuotao Fu 81*93ee0a75SLuotao Fu return sprintf(buf, "%u\n", val); 82*93ee0a75SLuotao Fu } 83*93ee0a75SLuotao Fu 84*93ee0a75SLuotao Fu static ssize_t mc13783_adc_read_gp(struct device *dev, 85*93ee0a75SLuotao Fu struct device_attribute *devattr, char *buf) 86*93ee0a75SLuotao Fu { 87*93ee0a75SLuotao Fu unsigned val; 88*93ee0a75SLuotao Fu int ret = mc13783_adc_read(dev, devattr, &val); 89*93ee0a75SLuotao Fu 90*93ee0a75SLuotao Fu if (ret) 91*93ee0a75SLuotao Fu return ret; 92*93ee0a75SLuotao Fu 93*93ee0a75SLuotao Fu /* 94*93ee0a75SLuotao Fu * input range is [0, 2.3V], val has 10 bits, so each bit 95*93ee0a75SLuotao Fu * is worth 9/4 mV. 96*93ee0a75SLuotao Fu */ 97*93ee0a75SLuotao Fu val = DIV_ROUND_CLOSEST(val * 9, 4); 98*93ee0a75SLuotao Fu 99*93ee0a75SLuotao Fu return sprintf(buf, "%u\n", val); 100*93ee0a75SLuotao Fu } 101*93ee0a75SLuotao Fu 102*93ee0a75SLuotao Fu static DEVICE_ATTR(name, S_IRUGO, mc13783_adc_show_name, NULL); 103*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, mc13783_adc_read_bp, NULL, 2); 104*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, mc13783_adc_read_gp, NULL, 5); 105*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, mc13783_adc_read_gp, NULL, 6); 106*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, mc13783_adc_read_gp, NULL, 7); 107*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, mc13783_adc_read_gp, NULL, 8); 108*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, mc13783_adc_read_gp, NULL, 9); 109*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, mc13783_adc_read_gp, NULL, 10); 110*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, mc13783_adc_read_gp, NULL, 11); 111*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, mc13783_adc_read_gp, NULL, 12); 112*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, mc13783_adc_read_gp, NULL, 13); 113*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, mc13783_adc_read_gp, NULL, 14); 114*93ee0a75SLuotao Fu static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, mc13783_adc_read_gp, NULL, 15); 115*93ee0a75SLuotao Fu 116*93ee0a75SLuotao Fu static struct attribute *mc13783_attr[] = { 117*93ee0a75SLuotao Fu &dev_attr_name.attr, 118*93ee0a75SLuotao Fu &sensor_dev_attr_in2_input.dev_attr.attr, 119*93ee0a75SLuotao Fu &sensor_dev_attr_in5_input.dev_attr.attr, 120*93ee0a75SLuotao Fu &sensor_dev_attr_in6_input.dev_attr.attr, 121*93ee0a75SLuotao Fu &sensor_dev_attr_in7_input.dev_attr.attr, 122*93ee0a75SLuotao Fu &sensor_dev_attr_in8_input.dev_attr.attr, 123*93ee0a75SLuotao Fu &sensor_dev_attr_in9_input.dev_attr.attr, 124*93ee0a75SLuotao Fu &sensor_dev_attr_in10_input.dev_attr.attr, 125*93ee0a75SLuotao Fu &sensor_dev_attr_in11_input.dev_attr.attr, 126*93ee0a75SLuotao Fu NULL 127*93ee0a75SLuotao Fu }; 128*93ee0a75SLuotao Fu 129*93ee0a75SLuotao Fu static const struct attribute_group mc13783_group = { 130*93ee0a75SLuotao Fu .attrs = mc13783_attr, 131*93ee0a75SLuotao Fu }; 132*93ee0a75SLuotao Fu 133*93ee0a75SLuotao Fu /* last four channels may be occupied by the touchscreen */ 134*93ee0a75SLuotao Fu static struct attribute *mc13783_attr_ts[] = { 135*93ee0a75SLuotao Fu &sensor_dev_attr_in12_input.dev_attr.attr, 136*93ee0a75SLuotao Fu &sensor_dev_attr_in13_input.dev_attr.attr, 137*93ee0a75SLuotao Fu &sensor_dev_attr_in14_input.dev_attr.attr, 138*93ee0a75SLuotao Fu &sensor_dev_attr_in15_input.dev_attr.attr, 139*93ee0a75SLuotao Fu NULL 140*93ee0a75SLuotao Fu }; 141*93ee0a75SLuotao Fu 142*93ee0a75SLuotao Fu static const struct attribute_group mc13783_group_ts = { 143*93ee0a75SLuotao Fu .attrs = mc13783_attr_ts, 144*93ee0a75SLuotao Fu }; 145*93ee0a75SLuotao Fu 146*93ee0a75SLuotao Fu static int __init mc13783_adc_probe(struct platform_device *pdev) 147*93ee0a75SLuotao Fu { 148*93ee0a75SLuotao Fu struct mc13783_adc_priv *priv; 149*93ee0a75SLuotao Fu int ret; 150*93ee0a75SLuotao Fu 151*93ee0a75SLuotao Fu priv = kzalloc(sizeof(*priv), GFP_KERNEL); 152*93ee0a75SLuotao Fu if (!priv) 153*93ee0a75SLuotao Fu return -ENOMEM; 154*93ee0a75SLuotao Fu 155*93ee0a75SLuotao Fu priv->mc13783 = dev_get_drvdata(pdev->dev.parent); 156*93ee0a75SLuotao Fu 157*93ee0a75SLuotao Fu platform_set_drvdata(pdev, priv); 158*93ee0a75SLuotao Fu 159*93ee0a75SLuotao Fu /* Register sysfs hooks */ 160*93ee0a75SLuotao Fu ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group); 161*93ee0a75SLuotao Fu if (ret) 162*93ee0a75SLuotao Fu goto out_err_create1; 163*93ee0a75SLuotao Fu 164*93ee0a75SLuotao Fu if (!(priv->mc13783->flags & MC13783_USE_TOUCHSCREEN)) 165*93ee0a75SLuotao Fu ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_ts); 166*93ee0a75SLuotao Fu if (ret) 167*93ee0a75SLuotao Fu goto out_err_create2; 168*93ee0a75SLuotao Fu 169*93ee0a75SLuotao Fu priv->hwmon_dev = hwmon_device_register(&pdev->dev); 170*93ee0a75SLuotao Fu if (IS_ERR(priv->hwmon_dev)) { 171*93ee0a75SLuotao Fu ret = PTR_ERR(priv->hwmon_dev); 172*93ee0a75SLuotao Fu dev_err(&pdev->dev, 173*93ee0a75SLuotao Fu "hwmon_device_register failed with %d.\n", ret); 174*93ee0a75SLuotao Fu goto out_err_register; 175*93ee0a75SLuotao Fu } 176*93ee0a75SLuotao Fu 177*93ee0a75SLuotao Fu 178*93ee0a75SLuotao Fu return 0; 179*93ee0a75SLuotao Fu 180*93ee0a75SLuotao Fu out_err_register: 181*93ee0a75SLuotao Fu 182*93ee0a75SLuotao Fu if (!(priv->mc13783->flags & MC13783_USE_TOUCHSCREEN)) 183*93ee0a75SLuotao Fu sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts); 184*93ee0a75SLuotao Fu out_err_create2: 185*93ee0a75SLuotao Fu 186*93ee0a75SLuotao Fu sysfs_remove_group(&pdev->dev.kobj, &mc13783_group); 187*93ee0a75SLuotao Fu out_err_create1: 188*93ee0a75SLuotao Fu 189*93ee0a75SLuotao Fu platform_set_drvdata(pdev, NULL); 190*93ee0a75SLuotao Fu kfree(priv); 191*93ee0a75SLuotao Fu 192*93ee0a75SLuotao Fu return ret; 193*93ee0a75SLuotao Fu } 194*93ee0a75SLuotao Fu 195*93ee0a75SLuotao Fu static int __devexit mc13783_adc_remove(struct platform_device *pdev) 196*93ee0a75SLuotao Fu { 197*93ee0a75SLuotao Fu struct mc13783_adc_priv *priv = platform_get_drvdata(pdev); 198*93ee0a75SLuotao Fu 199*93ee0a75SLuotao Fu hwmon_device_unregister(priv->hwmon_dev); 200*93ee0a75SLuotao Fu 201*93ee0a75SLuotao Fu if (!(priv->mc13783->flags & MC13783_USE_TOUCHSCREEN)) 202*93ee0a75SLuotao Fu sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts); 203*93ee0a75SLuotao Fu 204*93ee0a75SLuotao Fu sysfs_remove_group(&pdev->dev.kobj, &mc13783_group); 205*93ee0a75SLuotao Fu 206*93ee0a75SLuotao Fu platform_set_drvdata(pdev, NULL); 207*93ee0a75SLuotao Fu kfree(priv); 208*93ee0a75SLuotao Fu 209*93ee0a75SLuotao Fu return 0; 210*93ee0a75SLuotao Fu } 211*93ee0a75SLuotao Fu 212*93ee0a75SLuotao Fu static struct platform_driver mc13783_adc_driver = { 213*93ee0a75SLuotao Fu .remove = __devexit_p(mc13783_adc_remove), 214*93ee0a75SLuotao Fu .driver = { 215*93ee0a75SLuotao Fu .owner = THIS_MODULE, 216*93ee0a75SLuotao Fu .name = MC13783_ADC_NAME, 217*93ee0a75SLuotao Fu }, 218*93ee0a75SLuotao Fu }; 219*93ee0a75SLuotao Fu 220*93ee0a75SLuotao Fu static int __init mc13783_adc_init(void) 221*93ee0a75SLuotao Fu { 222*93ee0a75SLuotao Fu return platform_driver_probe(&mc13783_adc_driver, mc13783_adc_probe); 223*93ee0a75SLuotao Fu } 224*93ee0a75SLuotao Fu 225*93ee0a75SLuotao Fu static void __exit mc13783_adc_exit(void) 226*93ee0a75SLuotao Fu { 227*93ee0a75SLuotao Fu platform_driver_unregister(&mc13783_adc_driver); 228*93ee0a75SLuotao Fu } 229*93ee0a75SLuotao Fu 230*93ee0a75SLuotao Fu module_init(mc13783_adc_init); 231*93ee0a75SLuotao Fu module_exit(mc13783_adc_exit); 232*93ee0a75SLuotao Fu 233*93ee0a75SLuotao Fu MODULE_DESCRIPTION("MC13783 ADC driver"); 234*93ee0a75SLuotao Fu MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>"); 235*93ee0a75SLuotao Fu MODULE_LICENSE("GPL"); 236*93ee0a75SLuotao Fu MODULE_ALIAS("platform:" MC13783_ADC_NAME); 237