| /* |
| * magwheel.c - Linux kernel driver for the Diamond scroll wheel |
| * It uses the Freescale Vybrid vf610 ADC driver (iio/adc/vf610_adc.c) as a |
| * template |
| * |
| * ==Requirements== |
| * This driver should read two voltages every 2ms or less. It should then: |
| * - center those values around 0 |
| * - calculate current_angle = atan(val1/val2) |
| * - find the smallest change the wheel would have had to make to reflect that |
| * degree change. |
| * Assuming atan was in the range 0 to 360, 350 to 10 is a +20 degree |
| * change. |
| * 10 to 350 is a -20 degree change (because a +340 degree change is |
| * larger, and so we don't pick that). |
| * |
| * We can read the output of this to find the movement of the magwheel |
| * |
| * ==Notes on requirements== |
| * - The current_angle calculated above is calculating the direction of |
| * magnetic field |
| * - If we read voltages at longer intervals than 2ms, there's a danger that |
| * the user may spin the device fast enough to give us incorrect readings. |
| * In the assumption that "the smallest change is the real change", imagine |
| * if a user jumped from 10 to 350 degrees between the times that we |
| * polled. We would think they moved 20 degrees backwards. So we need to be |
| * sampling at greater than twice per period to understand what the user is |
| * doing (yay Nyquist)! |
| * - The atan calculation comes from the specsheet of the magnetic sensor |
| * - Centering values around 0 means shifting the readings |
| * by -(max_reading + min_reading) / 2. |
| * |
| * Copyright (C) 2010 Nest Labs, Inc |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License as published by |
| * the Free Software Foundation; either version 2 of the License, or |
| * (at your option) any later version. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| */ |
| |
| #include <linux/module.h> |
| #include <linux/platform_device.h> |
| #include <linux/interrupt.h> |
| #include <linux/delay.h> |
| #include <linux/kernel.h> |
| #include <linux/slab.h> |
| #include <linux/io.h> |
| #include <linux/clk.h> |
| #include <linux/completion.h> |
| #include <linux/of.h> |
| #include <linux/of_irq.h> |
| #include <linux/regulator/consumer.h> |
| #include <linux/of_platform.h> |
| #include <linux/err.h> |
| #include <linux/input-polldev.h> |
| #include <linux/bitops.h> |
| #include <linux/of_gpio.h> |
| #include <linux/gpio/consumer.h> |
| |
| #include <linux/iio/iio.h> |
| #include <linux/iio/sysfs.h> |
| #include <linux/iio/driver.h> |
| |
| /* This will be the driver name the kernel reports */ |
| #define DRIVER_NAME "vf610-adc-magwheel" |
| |
| /* Vybrid/IMX ADC registers */ |
| #define VF610_REG_ADC_HC0 0x00 |
| #define VF610_REG_ADC_HC1 0x04 |
| #define VF610_REG_ADC_HS 0x08 |
| #define VF610_REG_ADC_R0 0x0c |
| #define VF610_REG_ADC_R1 0x10 |
| #define VF610_REG_ADC_CFG 0x14 |
| #define VF610_REG_ADC_GC 0x18 |
| #define VF610_REG_ADC_GS 0x1c |
| #define VF610_REG_ADC_CV 0x20 |
| #define VF610_REG_ADC_OFS 0x24 |
| #define VF610_REG_ADC_CAL 0x28 |
| #define VF610_REG_ADC_PCTL 0x30 |
| |
| /* Configuration register field define */ |
| #define VF610_ADC_MODE_BIT8 0x00 |
| #define VF610_ADC_MODE_BIT10 0x04 |
| #define VF610_ADC_MODE_BIT12 0x08 |
| #define VF610_ADC_MODE_MASK 0x0c |
| #define VF610_ADC_BUSCLK_SEL 0x00 |
| #define VF610_ADC_BUSCLK2_SEL 0x01 |
| #define VF610_ADC_ALTCLK_SEL 0x02 |
| #define VF610_ADC_ADACK_SEL 0x03 |
| #define VF610_ADC_ADCCLK_MASK 0x03 |
| #define VF610_ADC_CLK_DIV2 0x20 |
| #define VF610_ADC_CLK_DIV4 0x40 |
| #define VF610_ADC_CLK_DIV8 0x60 |
| #define VF610_ADC_CLK_MASK 0x60 |
| #define VF610_ADC_ADLSMP_LONG 0x10 |
| #define VF610_ADC_ADSTS_MASK 0x300 |
| #define VF610_ADC_ADLPC_EN 0x80 |
| #define VF610_ADC_ADHSC_EN 0x400 |
| #define VF610_ADC_REFSEL_VREF 0x0 |
| #define VF610_ADC_REFSEL_VALT 0x100 |
| #define VF610_ADC_REFSEL_VBG 0x1000 |
| #define VF610_ADC_ADTRG_HARD 0x2000 |
| #define VF610_ADC_AVGS_8 0x4000 |
| #define VF610_ADC_AVGS_16 0x8000 |
| #define VF610_ADC_AVGS_32 0xC000 |
| #define VF610_ADC_AVGS_MASK 0xC000 |
| #define VF610_ADC_OVWREN 0x10000 |
| |
| /* General control register field define */ |
| #define VF610_ADC_ADACKEN 0x1 |
| #define VF610_ADC_DMAEN 0x2 |
| #define VF610_ADC_ACREN 0x4 |
| #define VF610_ADC_ACFGT 0x8 |
| #define VF610_ADC_ACFE 0x10 |
| #define VF610_ADC_AVGEN 0x20 |
| #define VF610_ADC_ADCON 0x40 |
| #define VF610_ADC_CAL 0x80 |
| |
| /* Comparators */ |
| #define VF610_ADC_ACFE 0x10 |
| #define VF610_ADC_ACFGT 0x8 |
| #define VF610_ADC_ACREN 0x4 |
| #define VF610_ADC_CV1 0x0000FFF |
| #define VF610_ADC_CV2 0xFFF0000 |
| #define VF610_ADC_CV2_SHIFT 16 |
| |
| /* Other field define */ |
| #define VF610_ADC_ADCHC(x) ((x) & 0x1F) |
| #define VF610_ADC_AIEN (0x1 << 7) |
| #define VF610_ADC_CONV_DISABLE 0x1F |
| #define VF610_ADC_HS_COCO0 0x1 |
| #define VF610_ADC_CALF 0x2 |
| #define VF610_ADC_TIMEOUT msecs_to_jiffies(100) |
| |
| /* For atan */ |
| #define ATAN_HYPO_MULTIPLIER 5 |
| |
| #define BITS_MASK_12 0xFFF |
| #define MAX_ADC_VAL 0xFFF |
| #define MIN_ADC_VAL 0 |
| #define MAX_INT 0x7ffffff |
| #define COMPARATOR_WINDOW_INIT 10 |
| #define ENABLE_DELAY_MIN_US 8000 |
| #define ENABLE_DELAY_MAX_US 12000 |
| |
| #define VF610_ADC_CHAN(_idx, _chan_type) { \ |
| .type = (_chan_type), \ |
| .indexed = 1, \ |
| .channel = (_idx), \ |
| .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
| BIT(IIO_CHAN_INFO_CALIBSCALE) | \ |
| BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| } |
| static const struct iio_chan_spec vf610_adc_iio_channels[] = { |
| VF610_ADC_CHAN(0, IIO_VOLTAGE), |
| VF610_ADC_CHAN(1, IIO_VOLTAGE), |
| VF610_ADC_CHAN(2, IIO_VOLTAGE), |
| VF610_ADC_CHAN(3, IIO_VOLTAGE), |
| VF610_ADC_CHAN(4, IIO_VOLTAGE), |
| VF610_ADC_CHAN(5, IIO_VOLTAGE), |
| VF610_ADC_CHAN(6, IIO_VOLTAGE), |
| VF610_ADC_CHAN(7, IIO_VOLTAGE), |
| VF610_ADC_CHAN(8, IIO_VOLTAGE), |
| VF610_ADC_CHAN(9, IIO_VOLTAGE), |
| VF610_ADC_CHAN(10, IIO_VOLTAGE), |
| VF610_ADC_CHAN(11, IIO_VOLTAGE), |
| VF610_ADC_CHAN(12, IIO_VOLTAGE), |
| VF610_ADC_CHAN(13, IIO_VOLTAGE), |
| VF610_ADC_CHAN(14, IIO_VOLTAGE), |
| VF610_ADC_CHAN(15, IIO_VOLTAGE), |
| /* sentinel */ |
| }; |
| |
| #define ATAN_TABLE_SIZE 15 |
| static int tan_table[ATAN_TABLE_SIZE] = { |
| 11520, 6801, 3593, 1824, 916, 458, 229, |
| 115, 57, 28, 14, 7, 4, 2, 1 |
| }; |
| |
| /* |
| * Given that work_handler is running at at most every 10 jiffies |
| * (should normally be 2), and that the time between interrupts |
| * is around 380 microseconds, each buffer channel length should be **132**. |
| * |
| * -- Derivation -- |
| * 2 jiffies = 20ms; 20ms / 380 microseconds = 52. |
| * |
| * That is, 52 interrupts happen before the delayed_work "buffer consumer" runs |
| * |
| * I've also tested this with no load and the largest length the sum |
| * of the two buffers got to was 50. |
| * |
| * However, we won't be guaranteed delay_work hits exactly 2 jiffies; |
| * it might be more delayed under high load. |
| * |
| * To be safe, let's say it might take up to 10 jiffies. |
| * 100ms / 380 microseconds = 264. That's the sum of the buffers, |
| * so each channel needs just half of that -- 132. |
| * |
| * So **132** should be the buffer length |
| */ |
| #define BUFFER_LENGTH 132 |
| struct vf610_adc { |
| struct device *dev; |
| void __iomem *regs; |
| struct clk *clk; |
| |
| u32 vref_uv; |
| u32 value; |
| struct regulator *vref; |
| bool calibration; |
| |
| struct completion completion; |
| |
| int channel_index; |
| u32 channels[2]; |
| u32 channel_midpoints[2]; |
| |
| struct input_dev *input_dev; |
| int irq; |
| |
| /* calibration values */ |
| int scale_int; /* integer component*/ |
| int scale_micro; /* thousandths component */ |
| }; |
| |
| struct magwheel_work { |
| struct delayed_work work; |
| struct magwheel *mag_info; |
| }; |
| |
| struct magwheel { |
| struct vf610_adc adc_info; |
| struct iio_dev *indio_dev; |
| |
| /* The ADC resolution is 12 bits, so 16 bits is enough */ |
| int buffered_vals1[BUFFER_LENGTH]; |
| int buffered_vals2[BUFFER_LENGTH]; |
| int buffered_loc; |
| int buffered_loc_last; |
| int last_angle; |
| int comparator_window; |
| struct gpio_desc *enable_gpio; |
| struct magwheel_work readbuffer_work; |
| }; |
| |
| static void magwheel_set_window(int center, struct magwheel *mag_info); |
| static void magwheel_single_conversion_start(struct vf610_adc *adc_info, int channel); |
| static void magwheel_continuous_conversion_start(struct vf610_adc *adc_info); |
| |
| static void dump_regs(struct vf610_adc *adc_info){ |
| int HC0, HC1, HS, R0, R1, CFG, GC, GS, CV, OFS, CAL; |
| |
| HC0 = readl(adc_info->regs + VF610_REG_ADC_HC0); |
| HC1 = readl(adc_info->regs + VF610_REG_ADC_HC1); |
| HS = readl(adc_info->regs + VF610_REG_ADC_HS); |
| R0 = readl(adc_info->regs + VF610_REG_ADC_R0); |
| R1 = readl(adc_info->regs + VF610_REG_ADC_R1); |
| CFG = readl(adc_info->regs + VF610_REG_ADC_CFG); |
| GC = readl(adc_info->regs + VF610_REG_ADC_GC); |
| GS = readl(adc_info->regs + VF610_REG_ADC_GS); |
| CV = readl(adc_info->regs + VF610_REG_ADC_CV); |
| OFS = readl(adc_info->regs + VF610_REG_ADC_OFS); |
| CAL = readl(adc_info->regs + VF610_REG_ADC_CAL); |
| |
| printk("ADC:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", |
| HC0, HC1, HS, R0, R1, CFG, GC, GS, CV, OFS, CAL ); |
| } |
| |
| static ssize_t show_comparator_window(struct device *dev, |
| struct device_attribute *attr, char *buf); |
| |
| static ssize_t store_comparator_window(struct device *dev, |
| struct device_attribute *attr, |
| const char *buf, size_t len); |
| |
| static ssize_t store_hall_enable(struct device *dev, |
| struct device_attribute *attr, |
| const char *buf, size_t len); |
| |
| DEVICE_ATTR(comparator_window, S_IRUGO | S_IWUSR, show_comparator_window, |
| store_comparator_window); |
| DEVICE_ATTR(hall_enable, S_IWUSR, NULL , |
| store_hall_enable); |
| |
| static struct attribute *magwheel_attributes[] = { |
| &dev_attr_comparator_window.attr, |
| &dev_attr_hall_enable.attr, |
| NULL |
| }; |
| |
| static struct attribute_group magwheel_attribute_group = { |
| .attrs = magwheel_attributes |
| }; |
| |
| static ssize_t show_comparator_window(struct device *dev, |
| struct device_attribute *attr, char *buf) |
| { |
| struct magwheel *mag_info = dev_get_drvdata(dev); |
| |
| return sprintf(buf, "%d\n", mag_info->comparator_window); |
| } |
| |
| static ssize_t store_comparator_window(struct device *dev, |
| struct device_attribute *attr, |
| const char *buf, size_t len) |
| { |
| struct magwheel *mag_info = dev_get_drvdata(dev); |
| int window; |
| |
| sscanf(buf, "%d", &window); |
| |
| mag_info->comparator_window = window; |
| |
| magwheel_set_window(0, mag_info); |
| magwheel_continuous_conversion_start(&mag_info->adc_info); |
| |
| return strnlen(buf, len); |
| } |
| |
| static ssize_t store_hall_enable(struct device *dev, |
| struct device_attribute *attr, |
| const char *buf, size_t len) |
| { |
| struct magwheel *mag_info = dev_get_drvdata(dev); |
| int enable; |
| |
| if (sscanf(buf, "%d", &enable) == 1) |
| { |
| gpiod_set_value(mag_info->enable_gpio, enable); |
| } |
| return strnlen(buf, len); |
| } |
| |
| static void magwheel_cfg_post_set(struct vf610_adc *adc_info) |
| { |
| int cfg_data = 0; |
| int gc_data = 0; |
| |
| /* low power set for calibration */ |
| cfg_data |= VF610_ADC_ADLPC_EN; |
| |
| /* enable high speed for calibration */ |
| cfg_data |= VF610_ADC_ADHSC_EN; |
| |
| /* voltage reference */ |
| cfg_data |= VF610_ADC_REFSEL_VREF; |
| |
| /* data overwrite enable */ |
| cfg_data |= VF610_ADC_OVWREN; |
| |
| writel(cfg_data, adc_info->regs + VF610_REG_ADC_CFG); |
| writel(gc_data, adc_info->regs + VF610_REG_ADC_GC); |
| } |
| |
| static void magwheel_calibration(struct vf610_adc *adc_info) |
| { |
| int adc_gc, hc_cfg; |
| int timeout; |
| |
| if (adc_info->calibration == true) |
| return; |
| |
| /* enable calibration interrupt */ |
| hc_cfg = VF610_ADC_AIEN | VF610_ADC_CONV_DISABLE; |
| writel(hc_cfg, adc_info->regs + VF610_REG_ADC_HC0); |
| |
| adc_gc = readl(adc_info->regs + VF610_REG_ADC_GC); |
| writel(adc_gc | VF610_ADC_CAL, adc_info->regs + VF610_REG_ADC_GC); |
| |
| timeout = wait_for_completion_timeout |
| (&adc_info->completion, VF610_ADC_TIMEOUT); |
| if (timeout == 0) |
| dev_err(adc_info->dev, "Timeout for adc calibration\n"); |
| |
| adc_gc = readl(adc_info->regs + VF610_REG_ADC_GS); |
| if (adc_gc & VF610_ADC_CALF) |
| { |
| dev_err(adc_info->dev, "ADC calibration failed\n"); |
| adc_info->calibration = false; |
| } |
| else |
| { |
| adc_info->calibration = true; |
| } |
| } |
| static int magwheel_write_raw(struct iio_dev *indio_dev, |
| struct iio_chan_spec const *chan, |
| int val, |
| int val2, |
| long mask) |
| { |
| int ret; |
| |
| struct magwheel *mag_info = *(struct magwheel**) iio_priv(indio_dev); |
| struct vf610_adc *adc_info = &mag_info->adc_info; |
| |
| switch (mask) |
| { |
| case IIO_CHAN_INFO_CALIBSCALE: |
| adc_info->scale_int = val; |
| adc_info->scale_micro = val2; |
| ret = 0; |
| break; |
| |
| default: |
| ret = EINVAL; |
| |
| } |
| |
| return ret; |
| |
| } |
| |
| static int magwheel_read_single_raw(struct iio_dev *indio_dev, |
| struct iio_chan_spec const *chan, |
| int *val, |
| int *val2, |
| long mask) |
| { |
| int ret; |
| |
| struct magwheel *mag_info = *(struct magwheel**) iio_priv(indio_dev); |
| struct vf610_adc *adc_info = &mag_info->adc_info; |
| |
| switch (mask) { |
| case IIO_CHAN_INFO_RAW: |
| |
| mutex_lock(&indio_dev->mlock); |
| reinit_completion(&adc_info->completion); |
| |
| magwheel_single_conversion_start(adc_info, chan->channel); |
| |
| ret = wait_for_completion_interruptible_timeout |
| (&adc_info->completion, VF610_ADC_TIMEOUT); |
| |
| if (ret == 0) { |
| mutex_unlock(&indio_dev->mlock); |
| return -ETIMEDOUT; |
| } |
| if (ret < 0) { |
| mutex_unlock(&indio_dev->mlock); |
| return ret; |
| } |
| |
| *val = (adc_info->value * adc_info->scale_int) + ((adc_info->value * adc_info->scale_micro) / 1000000); |
| mutex_unlock(&indio_dev->mlock); |
| return IIO_VAL_INT; |
| |
| case IIO_CHAN_INFO_CALIBSCALE: |
| *val = adc_info->scale_int; |
| *val2 = adc_info->scale_micro; |
| |
| return IIO_VAL_INT_PLUS_MICRO; |
| |
| case IIO_CHAN_INFO_SCALE: |
| *val = adc_info->vref_uv / 1000; |
| *val2 = 12; |
| return IIO_VAL_FRACTIONAL_LOG2; |
| |
| case IIO_CHAN_INFO_SAMP_FREQ: |
| *val = 1941176; |
| *val2 = 0; |
| return IIO_VAL_INT; |
| |
| default: |
| return -EINVAL; |
| } |
| |
| } |
| |
| static void magwheel_single_conversion_start(struct vf610_adc *adc_info, int channel) |
| { |
| int adc_hc; |
| int adc_gc; |
| |
| /* set up single conversion */ |
| /* Average enable. No range comparison*/ |
| adc_gc = VF610_ADC_AVGEN; |
| writel(adc_gc, adc_info->regs + VF610_REG_ADC_GC); |
| |
| /* Start the single conversion */ |
| adc_info->channel_index = -1; |
| adc_hc = VF610_ADC_ADCHC(channel); |
| |
| adc_hc |= VF610_ADC_AIEN; |
| writel(adc_hc, adc_info->regs + VF610_REG_ADC_HC0); |
| } |
| |
| static void magwheel_continuous_conversion_cfg(struct vf610_adc *adc_info) |
| { |
| int adc_gc; |
| |
| /* Turn on continuous conversion */ |
| adc_gc = readl(adc_info->regs + VF610_REG_ADC_GC); |
| /* continuous enable, and trigger on a range enabled */ |
| adc_gc |= VF610_ADC_ADCON | VF610_ADC_ACFE | VF610_ADC_ACREN |
| | VF610_ADC_ACFGT; |
| writel(adc_gc, adc_info->regs + VF610_REG_ADC_GC); |
| } |
| |
| static void magwheel_continuous_conversion_start(struct vf610_adc *adc_info) |
| { |
| int hc_cfg; |
| |
| /* Start the continuous conversion */ |
| hc_cfg = VF610_ADC_ADCHC(adc_info->channels[adc_info->channel_index]); |
| hc_cfg |= VF610_ADC_AIEN; |
| writel(hc_cfg, adc_info->regs + VF610_REG_ADC_HC0); |
| } |
| |
| static void magwheel_continuous_conversion_init(struct vf610_adc *adc_info) |
| { |
| int adc_cv; |
| |
| magwheel_continuous_conversion_cfg(adc_info); |
| |
| /* |
| * Pick a comparator range such that the ADC is guaranteed to trigger. |
| * After this, the ISR will set a more strict compareator window, |
| * once it knows the current ADC value |
| */ |
| adc_cv = MIN_ADC_VAL + ((MAX_ADC_VAL) << VF610_ADC_CV2_SHIFT); |
| writel(adc_cv, adc_info->regs + VF610_REG_ADC_CV); |
| |
| magwheel_continuous_conversion_start(adc_info); |
| } |
| |
| static void magwheel_continuous_conversion_resume(struct vf610_adc *adc_info) |
| { |
| magwheel_continuous_conversion_cfg(adc_info); |
| magwheel_continuous_conversion_start(adc_info); |
| } |
| |
| static void magwheel_cfg_set(struct vf610_adc *adc_info) |
| { |
| int cfg_data; |
| |
| cfg_data = readl(adc_info->regs + VF610_REG_ADC_CFG); |
| |
| /* low power configuration */ |
| cfg_data |= VF610_ADC_ADLPC_EN; |
| |
| /* disable high speed */ |
| cfg_data &= ~VF610_ADC_ADHSC_EN; |
| |
| writel(cfg_data, adc_info->regs + VF610_REG_ADC_CFG); |
| } |
| |
| static void magwheel_sample_set(struct vf610_adc *adc_info) |
| { |
| int cfg_data, gc_data; |
| |
| cfg_data = readl(adc_info->regs + VF610_REG_ADC_CFG); |
| gc_data = readl(adc_info->regs + VF610_REG_ADC_GC); |
| |
| /* resolution mode */ |
| cfg_data &= ~VF610_ADC_MODE_MASK; |
| cfg_data |= VF610_ADC_MODE_BIT12; |
| |
| /* clock select and clock divider */ |
| cfg_data &= ~(VF610_ADC_CLK_MASK | VF610_ADC_ADCCLK_MASK); |
| cfg_data |= VF610_ADC_ADACK_SEL | VF610_ADC_CLK_DIV8; |
| |
| /* Use the long sample mode */ |
| cfg_data |= VF610_ADC_ADLSMP_LONG | VF610_ADC_ADSTS_MASK; |
| |
| /* update hardware average selection */ |
| cfg_data &= ~VF610_ADC_AVGS_MASK; |
| gc_data |= VF610_ADC_AVGEN; |
| cfg_data |= VF610_ADC_AVGS_32; |
| |
| writel(cfg_data, adc_info->regs + VF610_REG_ADC_CFG); |
| writel(gc_data, adc_info->regs + VF610_REG_ADC_GC); |
| } |
| |
| static void magwheel_hw_init(struct vf610_adc *adc_info, bool is_resume) |
| { |
| /* CFG: Feature set */ |
| magwheel_cfg_post_set(adc_info); |
| magwheel_sample_set(adc_info); |
| |
| /* adc calibration */ |
| magwheel_calibration(adc_info); |
| |
| /* CFG: power and speed set */ |
| magwheel_cfg_set(adc_info); |
| |
| if (is_resume) |
| magwheel_continuous_conversion_resume(adc_info); |
| else |
| magwheel_continuous_conversion_init(adc_info); |
| } |
| |
| static int magwheel_read_data(struct vf610_adc *adc_info) |
| { |
| int result; |
| |
| result = readl(adc_info->regs + VF610_REG_ADC_R0); |
| result &= BITS_MASK_12; |
| return result; |
| } |
| |
| static void magwheel_set_window(int center, struct magwheel *mag_info) |
| { |
| int adc_cv = center + mag_info->comparator_window + ((center - mag_info->comparator_window) << VF610_ADC_CV2_SHIFT); |
| writel(adc_cv, mag_info->adc_info.regs + VF610_REG_ADC_CV); |
| } |
| |
| static irqreturn_t magwheel_isr(int irq, void *dev_id) |
| { |
| struct magwheel *mag_info = (struct magwheel *)dev_id; |
| struct vf610_adc *adc_info = &mag_info->adc_info; |
| int coco; |
| int value = 0; |
| |
| coco = readl(adc_info->regs + VF610_REG_ADC_HS); |
| if (coco & VF610_ADC_HS_COCO0) { |
| //conversion complete.. This really should happen every time. |
| value = magwheel_read_data(adc_info); |
| complete(&adc_info->completion); |
| } |
| |
| if (adc_info->channel_index == 0) { |
| /* |
| * Don't interrupt again unless you are X away from |
| * the current ADC value |
| * Note we need to write to HC0 to have an effect on ADC_CV |
| */ |
| magwheel_set_window(value, mag_info); |
| |
| /* switch channels */ |
| mag_info->buffered_vals1[mag_info->buffered_loc] = value; |
| adc_info->channel_index = 1; |
| } else if(adc_info->channel_index == 1) { |
| mag_info->buffered_vals2[mag_info->buffered_loc] = value; |
| /* |
| * Only increment the location when switching |
| * back to channel 1 |
| */ |
| mag_info->buffered_loc = (mag_info->buffered_loc + 1) |
| % BUFFER_LENGTH; |
| adc_info->channel_index = 0; |
| } else { |
| /* must be single read channel. Save this and set up continuous again.*/ |
| |
| mag_info->adc_info.value = value; |
| magwheel_continuous_conversion_cfg(adc_info); |
| adc_info->channel_index = 0; |
| |
| complete(&adc_info->completion); |
| } |
| magwheel_continuous_conversion_start(adc_info); |
| |
| schedule_delayed_work( |
| (struct delayed_work *) &mag_info->readbuffer_work, 2); |
| |
| return IRQ_HANDLED; |
| } |
| |
| /* Fixed point version of atan2 in math.h. Off by no more than 2 degrees. |
| * Using methods described in |
| * http://bsvi.ru/uploads/CORDIC--_10EBA/cordic.pdf |
| */ |
| static int magwheel_atan2_fp(int y, int x) |
| { |
| int xn; |
| /* |
| * 256 = 1 degree, so 11520 = 45 degrees * 256. |
| * Allows us to do this in fixed point style. |
| */ |
| int sum_angle = 0; |
| int i; |
| |
| /* |
| * Make sure x and y are big enough to get decent precision; |
| * this only really matters when inputs are small (< 100) |
| */ |
| x = x << ATAN_HYPO_MULTIPLIER; |
| y = y << ATAN_HYPO_MULTIPLIER; |
| if (x < 0) { |
| x = -x; |
| y = -y; |
| sum_angle = 180 * 256; |
| } |
| for (i = 0; i < ATAN_TABLE_SIZE; i++) { |
| if (y > 0) { |
| xn = x + (y >> i); |
| y = y - (x >> i); |
| sum_angle += tan_table[i]; |
| } else if (y < 0) { |
| xn = x - (y >> i); |
| y = y + (x >> i); |
| sum_angle -= tan_table[i]; |
| } else { |
| break; |
| } |
| x = xn; |
| } |
| /* Quadrant III needs to be turned into negative numbers */ |
| if (sum_angle > (180 * 256)) |
| return -360 + (sum_angle / 256); |
| return sum_angle / 256; |
| } |
| |
| /* |
| * angle inputs are from -180 to 180 |
| * Returns the amount of degrees to change, positive or negative |
| * (i.e. new_angle is -10 degrees in front of last_angle) |
| * |
| * There are two possible answers, but we guess that the one with |
| * the least change is right |
| */ |
| static int get_angle_change(int last_angle, int new_angle) |
| { |
| /* The angle went forward this much */ |
| int change_needed_forward; |
| int change_needed_backward; |
| change_needed_forward = (new_angle + 360) - last_angle; |
| if (change_needed_forward >= 360) |
| change_needed_forward -= 360; |
| /* Or backwards this much */ |
| change_needed_backward = (last_angle + 360) - new_angle; |
| if (change_needed_backward >= 360) |
| change_needed_backward -= 360; |
| |
| /* Whichever is the least change is the answer */ |
| if (change_needed_forward < change_needed_backward) |
| return change_needed_forward; |
| return -change_needed_backward; |
| } |
| |
| static void wait_handler(struct work_struct *work) |
| { |
| int i; |
| int start, end; |
| int current_angle; |
| int angle_change; |
| int total_angle_change = 0; |
| /* Cast so we can get access to mag_info */ |
| struct magwheel_work *magwheel_work = (struct magwheel_work *) work; |
| struct magwheel *mag_info = magwheel_work->mag_info; |
| struct vf610_adc *adc_info = &mag_info->adc_info; |
| struct input_dev *input = adc_info->input_dev; |
| |
| |
| /* get min/max */ |
| start = mag_info->buffered_loc_last; |
| /* don't need to lock here b/c we are just reading */ |
| end = mag_info->buffered_loc; |
| for (i = start; i != end; i = (i + 1) % BUFFER_LENGTH) { |
| /* Also calculate what the new angle should be */ |
| current_angle = magwheel_atan2_fp( |
| mag_info->buffered_vals2[i] |
| - adc_info->channel_midpoints[1], |
| mag_info->buffered_vals1[i] |
| - adc_info->channel_midpoints[0]); |
| angle_change = |
| get_angle_change(mag_info->last_angle, current_angle); |
| total_angle_change += angle_change; |
| mag_info->last_angle = current_angle; |
| } |
| mag_info->buffered_loc_last = end; |
| |
| pm_wakeup_event(adc_info->dev, 0); |
| |
| input_report_rel(input, REL_DIAL, total_angle_change); |
| input_sync(input); |
| } |
| |
| |
| static const struct of_device_id magwheel_match[] = { |
| { .compatible = "nestlabs,vf610-adc-magwheel", }, |
| { /* sentinel */ } |
| }; |
| MODULE_DEVICE_TABLE(of, magwheel_match); |
| |
| static const struct iio_info magwheel_iio_info = { |
| .driver_module = THIS_MODULE, |
| .read_raw = &magwheel_read_single_raw, |
| .write_raw = &magwheel_write_raw, |
| }; |
| |
| static int magwheel_probe(struct platform_device *pdev) |
| { |
| struct vf610_adc *adc_info; |
| struct magwheel *mag_info; |
| struct resource *mem; |
| int err; |
| struct input_dev *input; |
| int size = sizeof(struct magwheel); |
| |
| mag_info = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
| if (!mag_info) |
| return -ENOMEM; |
| |
| /* Turn on the hall sensor */ |
| mag_info->enable_gpio = devm_gpiod_get(&pdev->dev, "hall-sensor-enable"); |
| if (!IS_ERR(mag_info->enable_gpio)) { |
| gpiod_direction_output(mag_info->enable_gpio, 1); |
| gpiod_set_value(mag_info->enable_gpio, 1); |
| |
| dev_info(&pdev->dev, "register hall-sensor-enable-gpio\n"); |
| /* Wait to charge up */ |
| usleep_range(ENABLE_DELAY_MIN_US, ENABLE_DELAY_MAX_US); |
| } else { |
| dev_info(&pdev->dev, "fail to register hall-sensor-enable-gpio %d\n", PTR_ERR(mag_info->enable_gpio)); |
| mag_info->enable_gpio = NULL; |
| } |
| |
| mag_info->comparator_window = COMPARATOR_WINDOW_INIT; |
| |
| mag_info->indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct magwheel*)); |
| if (!mag_info->indio_dev) { |
| dev_err(&pdev->dev, "Failed allocating iio device\n"); |
| return -ENOMEM; |
| } |
| |
| *(struct magwheel**) iio_priv(mag_info->indio_dev) = mag_info; |
| |
| mag_info->indio_dev->name = dev_name(&pdev->dev); |
| mag_info->indio_dev->dev.parent = &pdev->dev; |
| mag_info->indio_dev->dev.of_node = pdev->dev.of_node; |
| mag_info->indio_dev->info = &magwheel_iio_info; |
| mag_info->indio_dev->modes = INDIO_DIRECT_MODE; |
| mag_info->indio_dev->channels = vf610_adc_iio_channels; |
| mag_info->indio_dev->num_channels = ARRAY_SIZE(vf610_adc_iio_channels); |
| |
| err = iio_device_register(mag_info->indio_dev); |
| |
| adc_info = &mag_info->adc_info; |
| adc_info->scale_int = 1; |
| adc_info->scale_micro = 0; |
| |
| |
| if (of_property_read_u32_array(pdev->dev.of_node, |
| "nestlabs,adc-channels-used", adc_info->channels, 2)) { |
| dev_err(&pdev->dev, |
| "adc-channels-used property in the DT needs to be a 2 element u32 array.\n"); |
| return -EINVAL; |
| } |
| |
| if (of_property_read_u32_array(pdev->dev.of_node, |
| "nestlabs,adc-channel-midpoints", |
| adc_info->channel_midpoints, 2)) { |
| dev_err(&pdev->dev, |
| "adc-channel-midpoints property in the DT needs to be a 2 element u32 array.\n"); |
| return -EINVAL; |
| } |
| |
| INIT_DELAYED_WORK((struct delayed_work *) &mag_info->readbuffer_work, |
| wait_handler); |
| |
| input = input_allocate_device(); |
| if (!mag_info || !input) { |
| dev_err(&pdev->dev, "Not enough memory for this driver!\n"); |
| return -ENOMEM; |
| } |
| adc_info->input_dev = input; |
| adc_info->channel_index = 0; |
| |
| input->name = pdev->name; |
| input->id.bustype = BUS_HOST; |
| input->dev.parent = &pdev->dev; |
| input_set_capability(input, EV_REL, REL_DIAL); |
| |
| /* Yes, mag_info has a readbuffer which points back to mag_info. |
| * That's because the work handler only gets readbuffer_work, |
| * and we want a pointer to mag_info |
| */ |
| mag_info->readbuffer_work.mag_info = mag_info; |
| adc_info->dev = &pdev->dev; |
| |
| device_init_wakeup(&pdev->dev, 1); |
| |
| mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| if (mem == NULL) { |
| dev_err(&pdev->dev, "found no memory resource\n"); |
| return -ENXIO; |
| } |
| |
| adc_info->regs = devm_ioremap_resource(&pdev->dev, mem); |
| if (IS_ERR(adc_info->regs)) |
| return PTR_ERR(adc_info->regs); |
| |
| adc_info->irq = platform_get_irq(pdev, 0); |
| if (adc_info->irq <= 0) { |
| dev_err(&pdev->dev, "no irq resource?\n"); |
| return -EINVAL; |
| } |
| |
| err = devm_request_irq(adc_info->dev, adc_info->irq, |
| magwheel_isr, 0, |
| dev_name(&pdev->dev), adc_info); |
| if (err < 0) { |
| dev_err(&pdev->dev, |
| "failed requesting irq, irq = %d\n", adc_info->irq); |
| return err; |
| } |
| |
| adc_info->clk = devm_clk_get(&pdev->dev, "adc"); |
| if (IS_ERR(adc_info->clk)) { |
| dev_err(&pdev->dev, "failed getting clock, err = %ld\n", |
| PTR_ERR(adc_info->clk)); |
| err = PTR_ERR(adc_info->clk); |
| return err; |
| } |
| |
| adc_info->vref = devm_regulator_get(&pdev->dev, "vref"); |
| if (IS_ERR(adc_info->vref)) |
| return PTR_ERR(adc_info->vref); |
| |
| err = regulator_enable(adc_info->vref); |
| if (err) |
| return err; |
| |
| adc_info->vref_uv = regulator_get_voltage(adc_info->vref); |
| |
| platform_set_drvdata(pdev, adc_info); |
| |
| init_completion(&adc_info->completion); |
| |
| magwheel_hw_init(adc_info, false); |
| |
| err = clk_prepare_enable(adc_info->clk); |
| if (err) { |
| dev_err(&pdev->dev, |
| "Could not prepare or enable the clock.\n"); |
| goto error_adc_clk_enable; |
| } |
| |
| err = input_register_device(input); |
| if (err) { |
| dev_err(&pdev->dev, "poll not working\n"); |
| goto error_input_register; |
| } |
| |
| err = sysfs_create_group(&pdev->dev.kobj, &magwheel_attribute_group); |
| if (err < 0) { |
| dev_err(&pdev->dev, "failed to create sysfs attributes\n"); |
| goto error_sysfs_register; |
| } |
| |
| return 0; |
| |
| error_sysfs_register: |
| input_unregister_device(adc_info->input_dev); |
| |
| error_input_register: |
| clk_disable_unprepare(adc_info->clk); |
| |
| error_adc_clk_enable: |
| platform_set_drvdata(pdev, NULL); |
| regulator_disable(adc_info->vref); |
| |
| return err; |
| } |
| |
| static int magwheel_remove(struct platform_device *pdev) |
| { |
| struct magwheel *mag_info = platform_get_drvdata(pdev); |
| struct vf610_adc *adc_info = &mag_info->adc_info; |
| |
| device_init_wakeup(adc_info->dev, 0); |
| |
| sysfs_remove_group(&pdev->dev.kobj, &magwheel_attribute_group); |
| input_unregister_device(adc_info->input_dev); |
| platform_set_drvdata(pdev, NULL); |
| clk_disable_unprepare(adc_info->clk); |
| regulator_disable(adc_info->vref); |
| |
| return 0; |
| } |
| |
| #ifdef CONFIG_PM_SLEEP |
| static int magwheel_suspend(struct device *dev) |
| { |
| struct magwheel *mag_info = dev_get_drvdata(dev); |
| struct vf610_adc *adc_info = &mag_info->adc_info; |
| int hc_cfg; |
| |
| /* ADC controller enters to stop mode */ |
| hc_cfg = readl(adc_info->regs + VF610_REG_ADC_HC0); |
| hc_cfg |= VF610_ADC_CONV_DISABLE; |
| writel(hc_cfg, adc_info->regs + VF610_REG_ADC_HC0); |
| |
| clk_disable_unprepare(adc_info->clk); |
| regulator_disable(adc_info->vref); |
| |
| /* Turn off the hall sensor */ |
| gpiod_set_value(mag_info->enable_gpio, 0); |
| |
| return 0; |
| } |
| |
| static int magwheel_resume(struct device *dev) |
| { |
| struct magwheel *mag_info = dev_get_drvdata(dev); |
| struct vf610_adc *adc_info = &mag_info->adc_info; |
| int ret; |
| |
| /* Turn on the hall sensor */ |
| gpiod_set_value(mag_info->enable_gpio, 1); |
| if (mag_info->enable_gpio) { |
| /* Wait to charge up */ |
| usleep_range(ENABLE_DELAY_MIN_US, ENABLE_DELAY_MAX_US); |
| } |
| |
| ret = clk_prepare_enable(adc_info->clk); |
| ret = regulator_enable(adc_info->vref); |
| if (ret) |
| return ret; |
| |
| magwheel_hw_init(adc_info, true); |
| |
| return 0; |
| } |
| #endif |
| |
| static SIMPLE_DEV_PM_OPS(magwheel_pm_ops, |
| magwheel_suspend, |
| magwheel_resume); |
| |
| static struct platform_driver magwheel_driver = { |
| .probe = magwheel_probe, |
| .remove = magwheel_remove, |
| .driver = { |
| .name = DRIVER_NAME, |
| .owner = THIS_MODULE, |
| .of_match_table = magwheel_match, |
| .pm = &magwheel_pm_ops, |
| }, |
| }; |
| |
| module_platform_driver(magwheel_driver); |
| |
| MODULE_AUTHOR("Chase Lambert <clambert@nestlabs.com>"); |
| MODULE_DESCRIPTION("Nest Labs Magwheel Driver"); |
| MODULE_LICENSE("GPL v2"); |