| /* |
| * 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/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> |
| |
| |
| /* 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) & 0xF) |
| #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 100 |
| #define ENABLE_DELAY_MIN_US 8000 |
| #define ENABLE_DELAY_MAX_US 12000 |
| |
| |
| #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; |
| |
| 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; |
| }; |
| |
| struct magwheel_work { |
| struct delayed_work work; |
| struct magwheel *mag_info; |
| }; |
| |
| struct magwheel { |
| struct vf610_adc adc_info; |
| |
| /* 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; |
| int enable_gpio; |
| struct magwheel_work readbuffer_work; |
| }; |
| |
| 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); |
| |
| DEVICE_ATTR(comparator_window, S_IRUGO | S_IWUSR, show_comparator_window, |
| store_comparator_window); |
| |
| static struct attribute *magwheel_attributes[] = { |
| &dev_attr_comparator_window.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; |
| |
| 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 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 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 adc_cv; |
| int value; |
| |
| coco = readl(adc_info->regs + VF610_REG_ADC_HS); |
| if (coco & VF610_ADC_HS_COCO0) { |
| adc_info->value = magwheel_read_data(adc_info); |
| complete(&adc_info->completion); |
| } |
| |
| value = adc_info->value; |
| 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 |
| */ |
| adc_cv = value + mag_info->comparator_window |
| + ((value - mag_info->comparator_window) |
| << VF610_ADC_CV2_SHIFT); |
| writel(adc_cv, adc_info->regs + VF610_REG_ADC_CV); |
| |
| /* switch channels */ |
| mag_info->buffered_vals1[mag_info->buffered_loc] = value; |
| adc_info->channel_index = 1; |
| } else { |
| 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; |
| } |
| 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 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 = of_get_named_gpio(pdev->dev.of_node, "hall-sensor-enable-gpio", 0); |
| if (gpio_is_valid(mag_info->enable_gpio)) { |
| devm_gpio_request_one(&pdev->dev, mag_info->enable_gpio, GPIOF_DIR_OUT, "hall-sensor-enable-gpio"); |
| gpio_set_value(mag_info->enable_gpio, 0); |
| dev_info(&pdev->dev, "register hall-sensor-enable-gpio %d\n", mag_info->enable_gpio); |
| |
| /* 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", mag_info->enable_gpio); |
| |
| mag_info->comparator_window = COMPARATOR_WINDOW_INIT; |
| |
| adc_info = &mag_info->adc_info; |
| |
| 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->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; |
| |
| platform_set_drvdata(pdev, adc_info); |
| |
| init_completion(&adc_info->completion); |
| |
| magwheel_hw_init(adc_info, false); |
| |
| err = input_register_device(input); |
| if (err) { |
| dev_err(&pdev->dev, "poll not working\n"); |
| goto fail; |
| } |
| |
| err = sysfs_create_group(&pdev->dev.kobj, &magwheel_attribute_group); |
| if (err < 0) { |
| dev_err(&pdev->dev, "failed to create sysfs attributes\n"); |
| goto unregister; |
| } |
| |
| return 0; |
| |
| unregister: |
| input_unregister_device(adc_info->input_dev); |
| |
| fail: |
| 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); |
| 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); |
| |
| regulator_disable(adc_info->vref); |
| |
| /* Turn off the hall sensor */ |
| if (gpio_is_valid(mag_info->enable_gpio)) |
| gpio_set_value(mag_info->enable_gpio, 1); |
| |
| 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 */ |
| if (gpio_is_valid(mag_info->enable_gpio)) { |
| gpio_set_value(mag_info->enable_gpio, 0); |
| |
| /* Wait to charge up */ |
| usleep_range(ENABLE_DELAY_MIN_US, ENABLE_DELAY_MAX_US); |
| } |
| |
| 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"); |