Linux Audio

Check our new training course

Loading...
v6.2
 1// SPDX-License-Identifier: GPL-2.0-or-later
 2/*
 3 * OpenFirmware bindings for the MMC-over-SPI driver
 4 *
 5 * Copyright (c) MontaVista Software, Inc. 2008.
 6 *
 7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
 
 
 
 
 
 8 */
 9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/device.h>
13#include <linux/slab.h>
14#include <linux/irq.h>
 
15#include <linux/of.h>
 
16#include <linux/of_irq.h>
17#include <linux/spi/spi.h>
18#include <linux/spi/mmc_spi.h>
19#include <linux/mmc/core.h>
20#include <linux/mmc/host.h>
21
 
 
 
 
 
22MODULE_LICENSE("GPL");
23
 
 
 
 
 
 
24struct of_mmc_spi {
25	struct mmc_spi_platform_data pdata;
 
26	int detect_irq;
 
27};
28
29static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
30{
31	return container_of(dev->platform_data, struct of_mmc_spi, pdata);
32}
33
34static int of_mmc_spi_init(struct device *dev,
35			   irqreturn_t (*irqhandler)(int, void *), void *mmc)
36{
37	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
38
39	return request_threaded_irq(oms->detect_irq, NULL, irqhandler,
40					IRQF_ONESHOT, dev_name(dev), mmc);
41}
42
43static void of_mmc_spi_exit(struct device *dev, void *mmc)
44{
45	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
46
47	free_irq(oms->detect_irq, mmc);
48}
49
50struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
51{
52	struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
53	struct device *dev = &spi->dev;
 
54	struct of_mmc_spi *oms;
 
 
 
 
55
56	if (dev->platform_data || !dev_fwnode(dev))
57		return dev->platform_data;
58
59	oms = kzalloc(sizeof(*oms), GFP_KERNEL);
60	if (!oms)
61		return NULL;
62
63	if (mmc_of_parse_voltage(mmc, &oms->pdata.ocr_mask) < 0)
 
 
 
64		goto err_ocr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
66	oms->detect_irq = spi->irq;
67	if (oms->detect_irq > 0) {
68		oms->pdata.init = of_mmc_spi_init;
69		oms->pdata.exit = of_mmc_spi_exit;
70	} else {
71		oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
72	}
73	if (device_property_read_bool(dev, "cap-sd-highspeed"))
74		oms->pdata.caps |= MMC_CAP_SD_HIGHSPEED;
75	if (device_property_read_bool(dev, "cap-mmc-highspeed"))
76		oms->pdata.caps |= MMC_CAP_MMC_HIGHSPEED;
77
78	dev->platform_data = &oms->pdata;
79	return dev->platform_data;
80err_ocr:
81	kfree(oms);
82	return NULL;
83}
84EXPORT_SYMBOL(mmc_spi_get_pdata);
85
86void mmc_spi_put_pdata(struct spi_device *spi)
87{
88	struct device *dev = &spi->dev;
 
89	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
90
91	if (!dev->platform_data || !dev_fwnode(dev))
92		return;
93
94	kfree(oms);
95	dev->platform_data = NULL;
96}
97EXPORT_SYMBOL(mmc_spi_put_pdata);
v3.15
 
  1/*
  2 * OpenFirmware bindings for the MMC-over-SPI driver
  3 *
  4 * Copyright (c) MontaVista Software, Inc. 2008.
  5 *
  6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7 *
  8 * This program is free software; you can redistribute  it and/or modify it
  9 * under  the terms of  the GNU General  Public License as published by the
 10 * Free Software Foundation;  either version 2 of the  License, or (at your
 11 * option) any later version.
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/device.h>
 17#include <linux/slab.h>
 18#include <linux/irq.h>
 19#include <linux/gpio.h>
 20#include <linux/of.h>
 21#include <linux/of_gpio.h>
 22#include <linux/of_irq.h>
 23#include <linux/spi/spi.h>
 24#include <linux/spi/mmc_spi.h>
 25#include <linux/mmc/core.h>
 26#include <linux/mmc/host.h>
 27
 28/* For archs that don't support NO_IRQ (such as mips), provide a dummy value */
 29#ifndef NO_IRQ
 30#define NO_IRQ 0
 31#endif
 32
 33MODULE_LICENSE("GPL");
 34
 35enum {
 36	CD_GPIO = 0,
 37	WP_GPIO,
 38	NUM_GPIOS,
 39};
 40
 41struct of_mmc_spi {
 42	int gpios[NUM_GPIOS];
 43	bool alow_gpios[NUM_GPIOS];
 44	int detect_irq;
 45	struct mmc_spi_platform_data pdata;
 46};
 47
 48static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
 49{
 50	return container_of(dev->platform_data, struct of_mmc_spi, pdata);
 51}
 52
 53static int of_mmc_spi_init(struct device *dev,
 54			   irqreturn_t (*irqhandler)(int, void *), void *mmc)
 55{
 56	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
 57
 58	return request_threaded_irq(oms->detect_irq, NULL, irqhandler, 0,
 59				    dev_name(dev), mmc);
 60}
 61
 62static void of_mmc_spi_exit(struct device *dev, void *mmc)
 63{
 64	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
 65
 66	free_irq(oms->detect_irq, mmc);
 67}
 68
 69struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
 70{
 
 71	struct device *dev = &spi->dev;
 72	struct device_node *np = dev->of_node;
 73	struct of_mmc_spi *oms;
 74	const u32 *voltage_ranges;
 75	int num_ranges;
 76	int i;
 77	int ret = -EINVAL;
 78
 79	if (dev->platform_data || !np)
 80		return dev->platform_data;
 81
 82	oms = kzalloc(sizeof(*oms), GFP_KERNEL);
 83	if (!oms)
 84		return NULL;
 85
 86	voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
 87	num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
 88	if (!voltage_ranges || !num_ranges) {
 89		dev_err(dev, "OF: voltage-ranges unspecified\n");
 90		goto err_ocr;
 91	}
 92
 93	for (i = 0; i < num_ranges; i++) {
 94		const int j = i * 2;
 95		u32 mask;
 96
 97		mask = mmc_vddrange_to_ocrmask(be32_to_cpu(voltage_ranges[j]),
 98					       be32_to_cpu(voltage_ranges[j + 1]));
 99		if (!mask) {
100			ret = -EINVAL;
101			dev_err(dev, "OF: voltage-range #%d is invalid\n", i);
102			goto err_ocr;
103		}
104		oms->pdata.ocr_mask |= mask;
105	}
106
107	for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
108		enum of_gpio_flags gpio_flags;
109
110		oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags);
111		if (!gpio_is_valid(oms->gpios[i]))
112			continue;
113
114		if (gpio_flags & OF_GPIO_ACTIVE_LOW)
115			oms->alow_gpios[i] = true;
116	}
117
118	if (gpio_is_valid(oms->gpios[CD_GPIO])) {
119		oms->pdata.cd_gpio = oms->gpios[CD_GPIO];
120		oms->pdata.flags |= MMC_SPI_USE_CD_GPIO;
121		if (!oms->alow_gpios[CD_GPIO])
122			oms->pdata.caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
123	}
124	if (gpio_is_valid(oms->gpios[WP_GPIO])) {
125		oms->pdata.ro_gpio = oms->gpios[WP_GPIO];
126		oms->pdata.flags |= MMC_SPI_USE_RO_GPIO;
127		if (!oms->alow_gpios[WP_GPIO])
128			oms->pdata.caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
129	}
130
131	oms->detect_irq = irq_of_parse_and_map(np, 0);
132	if (oms->detect_irq != 0) {
133		oms->pdata.init = of_mmc_spi_init;
134		oms->pdata.exit = of_mmc_spi_exit;
135	} else {
136		oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
137	}
 
 
 
 
138
139	dev->platform_data = &oms->pdata;
140	return dev->platform_data;
141err_ocr:
142	kfree(oms);
143	return NULL;
144}
145EXPORT_SYMBOL(mmc_spi_get_pdata);
146
147void mmc_spi_put_pdata(struct spi_device *spi)
148{
149	struct device *dev = &spi->dev;
150	struct device_node *np = dev->of_node;
151	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
152
153	if (!dev->platform_data || !np)
154		return;
155
156	kfree(oms);
157	dev->platform_data = NULL;
158}
159EXPORT_SYMBOL(mmc_spi_put_pdata);