@@ -0,0 +1,336 @@
+/*
+ * Sharp LoCoMo support
+ *
+ * (C) Copyright 2015 Dmitry Eremin-Solenikov
+ *
+ * Based on old driver at arch/arm/common/locomo.c
+ *
+ * (C) Copyright 2004 John Lenz
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This file contains all generic LoCoMo support.
+ *
+ * All initialization functions provided here are intended to be called
+ * from machine specific code with proper arguments when required.
+ */
+
+#include <linux/delay.h>
+#include <linux/gpio.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/irqdomain.h>
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/locomo.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+
+/* LoCoMo Interrupts */
+#define IRQ_LOCOMO_KEY 0
+#define IRQ_LOCOMO_GPIO 1
+#define IRQ_LOCOMO_LT 2
+#define IRQ_LOCOMO_SPI 3
+
+#define LOCOMO_NR_IRQS 4
+
+/* The following is the overall data for the locomo chip */
+struct locomo {
+ struct device *dev;
+ unsigned int irq;
+ spinlock_t lock;
+ struct irq_domain *domain;
+ struct regmap *regmap;
+};
+
+static struct resource locomo_kbd_resources[] = {
+ DEFINE_RES_IRQ(IRQ_LOCOMO_KEY),
+};
+
+static struct resource locomo_gpio_resources[] = {
+ DEFINE_RES_IRQ(IRQ_LOCOMO_GPIO),
+};
+
+/* Filled in locomo_probe() function, will be kmemduped by platform core */
+static struct locomo_gpio_platform_data locomo_gpio_pdata;
+
+static struct resource locomo_lt_resources[] = {
+ DEFINE_RES_IRQ(IRQ_LOCOMO_LT),
+};
+
+static struct resource locomo_spi_resources[] = {
+ DEFINE_RES_IRQ(IRQ_LOCOMO_SPI),
+};
+
+/* Filled in locomo_probe() function, will be kmemduped by platform core */
+static struct locomo_lcd_platform_data locomo_lcd_pdata;
+
+static struct mfd_cell locomo_cells[] = {
+ {
+ .name = "locomo-kbd",
+ .resources = locomo_kbd_resources,
+ .num_resources = ARRAY_SIZE(locomo_kbd_resources),
+ },
+ {
+ .name = "locomo-gpio",
+ .resources = locomo_gpio_resources,
+ .num_resources = ARRAY_SIZE(locomo_gpio_resources),
+ .platform_data = &locomo_gpio_pdata,
+ .pdata_size = sizeof(locomo_gpio_pdata),
+ },
+ {
+ .name = "locomo-lt", /* Long time timer */
+ .resources = locomo_lt_resources,
+ .num_resources = ARRAY_SIZE(locomo_lt_resources),
+ },
+ {
+ .name = "locomo-spi",
+ .resources = locomo_spi_resources,
+ .num_resources = ARRAY_SIZE(locomo_spi_resources),
+ },
+ {
+ .name = "locomo-lcd",
+ .platform_data = &locomo_lcd_pdata,
+ .pdata_size = sizeof(locomo_lcd_pdata),
+ },
+ { .name = "locomo-led", },
+ { .name = "locomo-backlight", },
+ { .name = "locomo-i2c", },
+};
+
+/*
+ * IRQ support
+ */
+static void locomo_handler(unsigned int irq, struct irq_desc *desc)
+{
+ struct locomo *ldev = irq_get_handler_data(irq);
+ struct irq_chip *irqchip = irq_desc_get_chip(desc);
+ unsigned int req;
+
+ chained_irq_enter(irqchip, desc);
+
+ /* Check why this interrupt was generated */
+ while (1) {
+ regmap_read(ldev->regmap, LOCOMO_ICR, &req);
+ req >>= LOCOMO_ICR_STATUS_SHIFT;
+
+ if (!req)
+ break;
+
+ irq = ffs(req) - 1;
+ generic_handle_irq(irq_find_mapping(ldev->domain, irq));
+ }
+
+ chained_irq_exit(irqchip, desc);
+}
+
+static void locomo_mask_irq(struct irq_data *d)
+{
+ struct locomo *ldev = irq_data_get_irq_chip_data(d);
+ unsigned int mask = BIT(d->hwirq + LOCOMO_ICR_MASK_SHIFT);
+
+ regmap_update_bits(ldev->regmap, LOCOMO_ICR, mask, 0);
+}
+
+static void locomo_unmask_irq(struct irq_data *d)
+{
+ struct locomo *ldev = irq_data_get_irq_chip_data(d);
+ unsigned int mask = BIT(d->hwirq + LOCOMO_ICR_MASK_SHIFT);
+
+ regmap_update_bits(ldev->regmap, LOCOMO_ICR, mask, mask);
+}
+
+static struct irq_chip locomo_chip = {
+ .name = "locomo",
+ .irq_mask = locomo_mask_irq,
+ .irq_unmask = locomo_unmask_irq,
+};
+
+static int locomo_irq_map(struct irq_domain *d, unsigned int virq,
+ irq_hw_number_t hwirq)
+{
+ struct locomo *locomo = d->host_data;
+
+ irq_set_chip_data(virq, locomo);
+ irq_set_chip_and_handler(virq, &locomo_chip, handle_level_irq);
+ set_irq_flags(virq, IRQF_VALID);
+
+ return 0;
+}
+
+static void locomo_irq_unmap(struct irq_domain *d, unsigned int virq)
+{
+ set_irq_flags(virq, 0);
+ irq_set_chip_and_handler(virq, NULL, NULL);
+ irq_set_chip_data(virq, NULL);
+}
+
+static struct irq_domain_ops locomo_irq_ops = {
+ .map = locomo_irq_map,
+ .unmap = locomo_irq_unmap,
+ .xlate = irq_domain_xlate_onecell,
+};
+
+static int locomo_setup_irq(struct locomo *ldev)
+{
+ ldev->domain = irq_domain_add_simple(NULL, LOCOMO_NR_IRQS, 0,
+ &locomo_irq_ops, ldev);
+ if (!ldev->domain)
+ return -ENOMEM;
+
+ /*
+ * Install handler for IRQ_LOCOMO_HW.
+ */
+ irq_set_irq_type(ldev->irq, IRQ_TYPE_EDGE_FALLING);
+ irq_set_handler_data(ldev->irq, ldev);
+ irq_set_chained_handler(ldev->irq, locomo_handler);
+
+ return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int locomo_suspend(struct device *dev)
+{
+ struct locomo *ldev = dev_get_drvdata(dev);
+
+ /* Audio */
+ regmap_write(ldev->regmap, LOCOMO_PAIF, 0x00);
+
+ /*
+ * Original code disabled the clock depending on leds settings
+ * However we disable leds before suspend, thus it's safe
+ * to just assume this setting.
+ */
+ /* CLK32 off */
+ regmap_write(ldev->regmap, LOCOMO_C32K, 0x00);
+
+ /* 22MHz/24MHz clock off */
+ regmap_write(ldev->regmap, LOCOMO_ACC, 0x00);
+
+ return 0;
+}
+
+static int locomo_resume(struct device *dev)
+{
+ struct locomo *ldev = dev_get_drvdata(dev);
+
+ regmap_write(ldev->regmap, LOCOMO_C32K, 0x00);
+
+ return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(locomo_pm, locomo_suspend, locomo_resume);
+
+static const struct regmap_config locomo_regmap_config = {
+ .name = "LoCoMo",
+ .reg_bits = 8,
+ .reg_stride = 4,
+ .val_bits = 16,
+ .cache_type = REGCACHE_NONE,
+ .max_register = 0xec,
+};
+
+static int locomo_probe(struct platform_device *pdev)
+{
+ struct locomo_platform_data *pdata = dev_get_platdata(&pdev->dev);
+ struct resource *res;
+ void __iomem *base;
+ struct locomo *ldev;
+ unsigned int rev;
+ int ret;
+
+ ldev = devm_kzalloc(&pdev->dev, sizeof(*ldev), GFP_KERNEL);
+ if (!ldev)
+ return -ENOMEM;
+
+ spin_lock_init(&ldev->lock);
+ ldev->dev = &pdev->dev;
+
+ ldev->irq = platform_get_irq(pdev, 0);
+ if (ldev->irq < 0)
+ return ldev->irq;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ base = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(base))
+ return PTR_ERR(base);
+
+ ldev->regmap = devm_regmap_init_mmio(&pdev->dev, base,
+ &locomo_regmap_config);
+ if (IS_ERR(ldev->regmap))
+ return PTR_ERR(ldev->regmap);
+
+ if (pdata) {
+ locomo_gpio_pdata.gpio_base = pdata->gpio_base;
+ locomo_lcd_pdata.comadj = pdata->comadj;
+ } else {
+ locomo_gpio_pdata.gpio_base = -1;
+ locomo_lcd_pdata.comadj = 128;
+ }
+
+ platform_set_drvdata(pdev, ldev);
+
+ regmap_read(ldev->regmap, LOCOMO_VER, &rev);
+ dev_info(&pdev->dev, "LoCoMo Chip: %04x\n", rev);
+
+ /* Clear IRQ status and mask */
+ regmap_write(ldev->regmap, LOCOMO_ICR, 0);
+
+ /* Longtime timer */
+ regmap_write(ldev->regmap, LOCOMO_LTINT, 0);
+
+ ret = locomo_setup_irq(ldev);
+ if (ret)
+ return ret;
+
+ ret = mfd_add_devices(&pdev->dev, pdev->id,
+ locomo_cells, ARRAY_SIZE(locomo_cells),
+ res, -1, ldev->domain);
+ if (ret)
+ goto err;
+
+ return 0;
+
+err:
+ irq_set_chained_handler(ldev->irq, NULL);
+ irq_set_handler_data(ldev->irq, NULL);
+
+ irq_domain_remove(ldev->domain);
+
+ return ret;
+}
+
+static int locomo_remove(struct platform_device *dev)
+{
+ struct locomo *ldev = platform_get_drvdata(dev);
+
+ mfd_remove_devices(&dev->dev);
+
+ irq_set_chained_handler(ldev->irq, NULL);
+ irq_set_handler_data(ldev->irq, NULL);
+
+ irq_domain_remove(ldev->domain);
+
+ return 0;
+}
+
+static struct platform_driver locomo_device_driver = {
+ .probe = locomo_probe,
+ .remove = locomo_remove,
+ .driver = {
+ .name = "locomo",
+ .pm = &locomo_pm,
+ },
+};
+
+module_platform_driver(locomo_device_driver);
+
+MODULE_DESCRIPTION("Sharp LoCoMo core driver");
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
+MODULE_ALIAS("platform:locomo");