···2020#include <linux/errno.h>2121#include <linux/err.h>2222#include <linux/interrupt.h>2323+#include <linux/kernel.h>2324#include <linux/platform_device.h>2425#include <linux/spi/pxa2xx_spi.h>2526#include <linux/spi/spi.h>···3029#include <linux/clk.h>3130#include <linux/pm_runtime.h>3231#include <linux/acpi.h>3333-3434-#include <asm/io.h>3535-#include <asm/irq.h>3636-#include <asm/delay.h>37323833#include "spi-pxa2xx.h"3934···6366#define LPSS_RX_THRESH_DFLT 646467#define LPSS_TX_LOTHRESH_DFLT 1606568#define LPSS_TX_HITHRESH_DFLT 2246666-6767-struct quark_spi_rate {6868- u32 bitrate;6969- u32 dds_clk_rate;7070- u32 clk_div;7171-};7272-7373-/*7474- * 'rate', 'dds', 'clk_div' lookup table, which is defined in7575- * the Quark SPI datasheet.7676- */7777-static const struct quark_spi_rate quark_spi_rate_table[] = {7878-/* bitrate, dds_clk_rate, clk_div */7979- {50000000, 0x800000, 0},8080- {40000000, 0x666666, 0},8181- {25000000, 0x400000, 0},8282- {20000000, 0x666666, 1},8383- {16667000, 0x800000, 2},8484- {13333000, 0x666666, 2},8585- {12500000, 0x200000, 0},8686- {10000000, 0x800000, 4},8787- {8000000, 0x666666, 4},8888- {6250000, 0x400000, 3},8989- {5000000, 0x400000, 4},9090- {4000000, 0x666666, 9},9191- {3125000, 0x80000, 0},9292- {2500000, 0x400000, 9},9393- {2000000, 0x666666, 19},9494- {1563000, 0x40000, 0},9595- {1250000, 0x200000, 9},9696- {1000000, 0x400000, 24},9797- {800000, 0x666666, 49},9898- {781250, 0x20000, 0},9999- {625000, 0x200000, 19},100100- {500000, 0x400000, 49},101101- {400000, 0x666666, 99},102102- {390625, 0x10000, 0},103103- {250000, 0x400000, 99},104104- {200000, 0x666666, 199},105105- {195313, 0x8000, 0},106106- {125000, 0x100000, 49},107107- {100000, 0x200000, 124},108108- {50000, 0x100000, 124},109109- {25000, 0x80000, 124},110110- {10016, 0x20000, 77},111111- {5040, 0x20000, 154},112112- {1002, 0x8000, 194},113113-};1146911570/* Offset from drv_data->lpss_base */11671#define GENERAL_REG 0x08···650701}651702652703/*653653- * The Quark SPI data sheet gives a table, and for the given 'rate',654654- * the 'dds' and 'clk_div' can be found in the table.704704+ * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply705705+ * input frequency by fractions of 2^24. It also has a divider by 5.706706+ *707707+ * There are formulas to get baud rate value for given input frequency and708708+ * divider parameters, such as DDS_CLK_RATE and SCR:709709+ *710710+ * Fsys = 200MHz711711+ *712712+ * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)713713+ * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)714714+ *715715+ * DDS_CLK_RATE either 2^n or 2^n / 5.716716+ * SCR is in range 0 .. 255717717+ *718718+ * Divisor = 5^i * 2^j * 2 * k719719+ * i = [0, 1] i = 1 iff j = 0 or j > 3720720+ * j = [0, 23] j = 0 iff i = 1721721+ * k = [1, 256]722722+ * Special case: j = 0, i = 1: Divisor = 2 / 5723723+ *724724+ * Accordingly to the specification the recommended values for DDS_CLK_RATE725725+ * are:726726+ * Case 1: 2^n, n = [0, 23]727727+ * Case 2: 2^24 * 2 / 5 (0x666666)728728+ * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)729729+ *730730+ * In all cases the lowest possible value is better.731731+ *732732+ * The function calculates parameters for all cases and chooses the one closest733733+ * to the asked baud rate.655734 */656656-static u32 quark_x1000_set_clk_regvals(u32 rate, u32 *dds, u32 *clk_div)735735+static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)657736{658658- unsigned int i;737737+ unsigned long xtal = 200000000;738738+ unsigned long fref = xtal / 2; /* mandatory division by 2,739739+ see (2) */740740+ /* case 3 */741741+ unsigned long fref1 = fref / 2; /* case 1 */742742+ unsigned long fref2 = fref * 2 / 5; /* case 2 */743743+ unsigned long scale;744744+ unsigned long q, q1, q2;745745+ long r, r1, r2;746746+ u32 mul;659747660660- for (i = 0; i < ARRAY_SIZE(quark_spi_rate_table); i++) {661661- if (rate >= quark_spi_rate_table[i].bitrate) {662662- *dds = quark_spi_rate_table[i].dds_clk_rate;663663- *clk_div = quark_spi_rate_table[i].clk_div;664664- return quark_spi_rate_table[i].bitrate;748748+ /* Case 1 */749749+750750+ /* Set initial value for DDS_CLK_RATE */751751+ mul = (1 << 24) >> 1;752752+753753+ /* Calculate initial quot */754754+ q1 = DIV_ROUND_CLOSEST(fref1, rate);755755+756756+ /* Scale q1 if it's too big */757757+ if (q1 > 256) {758758+ /* Scale q1 to range [1, 512] */759759+ scale = fls_long(q1 - 1);760760+ if (scale > 9) {761761+ q1 >>= scale - 9;762762+ mul >>= scale - 9;763763+ }764764+765765+ /* Round the result if we have a remainder */766766+ q1 += q1 & 1;767767+ }768768+769769+ /* Decrease DDS_CLK_RATE as much as we can without loss in precision */770770+ scale = __ffs(q1);771771+ q1 >>= scale;772772+ mul >>= scale;773773+774774+ /* Get the remainder */775775+ r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);776776+777777+ /* Case 2 */778778+779779+ q2 = DIV_ROUND_CLOSEST(fref2, rate);780780+ r2 = abs(fref2 / q2 - rate);781781+782782+ /*783783+ * Choose the best between two: less remainder we have the better. We784784+ * can't go case 2 if q2 is greater than 256 since SCR register can785785+ * hold only values 0 .. 255.786786+ */787787+ if (r2 >= r1 || q2 > 256) {788788+ /* case 1 is better */789789+ r = r1;790790+ q = q1;791791+ } else {792792+ /* case 2 is better */793793+ r = r2;794794+ q = q2;795795+ mul = (1 << 24) * 2 / 5;796796+ }797797+798798+ /* Check case 3 only If the divisor is big enough */799799+ if (fref / rate >= 80) {800800+ u64 fssp;801801+ u32 m;802802+803803+ /* Calculate initial quot */804804+ q1 = DIV_ROUND_CLOSEST(fref, rate);805805+ m = (1 << 24) / q1;806806+807807+ /* Get the remainder */808808+ fssp = (u64)fref * m;809809+ do_div(fssp, 1 << 24);810810+ r1 = abs(fssp - rate);811811+812812+ /* Choose this one if it suits better */813813+ if (r1 < r) {814814+ /* case 3 is better */815815+ q = 1;816816+ mul = m;665817 }666818 }667819668668- *dds = quark_spi_rate_table[i-1].dds_clk_rate;669669- *clk_div = quark_spi_rate_table[i-1].clk_div;670670-671671- return quark_spi_rate_table[i-1].bitrate;820820+ *dds = mul;821821+ return q - 1;672822}673823674824static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)···778730 rate = min_t(int, ssp_clk, rate);779731780732 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)781781- return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;733733+ return (ssp_clk / (2 * rate) - 1) & 0xff;782734 else783783- return ((ssp_clk / rate - 1) & 0xfff) << 8;735735+ return (ssp_clk / rate - 1) & 0xfff;784736}785737786738static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,787739 struct chip_data *chip, int rate)788740{789789- u32 clk_div;741741+ unsigned int clk_div;790742791743 switch (drv_data->ssp_type) {792744 case QUARK_X1000_SSP:793793- quark_x1000_set_clk_regvals(rate, &chip->dds_rate, &clk_div);794794- return clk_div << 8;745745+ clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);746746+ break;795747 default:796796- return ssp_get_clk_div(drv_data, rate);748748+ clk_div = ssp_get_clk_div(drv_data, rate);749749+ break;797750 }751751+ return clk_div << 8;798752}799753800754static void pump_transfers(unsigned long data)
-114
drivers/spi/spi.c
···128128 return 0;129129}130130131131-#ifdef CONFIG_PM_SLEEP132132-static int spi_legacy_suspend(struct device *dev, pm_message_t message)133133-{134134- int value = 0;135135- struct spi_driver *drv = to_spi_driver(dev->driver);136136-137137- /* suspend will stop irqs and dma; no more i/o */138138- if (drv) {139139- if (drv->suspend)140140- value = drv->suspend(to_spi_device(dev), message);141141- else142142- dev_dbg(dev, "... can't suspend\n");143143- }144144- return value;145145-}146146-147147-static int spi_legacy_resume(struct device *dev)148148-{149149- int value = 0;150150- struct spi_driver *drv = to_spi_driver(dev->driver);151151-152152- /* resume may restart the i/o queue */153153- if (drv) {154154- if (drv->resume)155155- value = drv->resume(to_spi_device(dev));156156- else157157- dev_dbg(dev, "... can't resume\n");158158- }159159- return value;160160-}161161-162162-static int spi_pm_suspend(struct device *dev)163163-{164164- const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;165165-166166- if (pm)167167- return pm_generic_suspend(dev);168168- else169169- return spi_legacy_suspend(dev, PMSG_SUSPEND);170170-}171171-172172-static int spi_pm_resume(struct device *dev)173173-{174174- const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;175175-176176- if (pm)177177- return pm_generic_resume(dev);178178- else179179- return spi_legacy_resume(dev);180180-}181181-182182-static int spi_pm_freeze(struct device *dev)183183-{184184- const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;185185-186186- if (pm)187187- return pm_generic_freeze(dev);188188- else189189- return spi_legacy_suspend(dev, PMSG_FREEZE);190190-}191191-192192-static int spi_pm_thaw(struct device *dev)193193-{194194- const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;195195-196196- if (pm)197197- return pm_generic_thaw(dev);198198- else199199- return spi_legacy_resume(dev);200200-}201201-202202-static int spi_pm_poweroff(struct device *dev)203203-{204204- const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;205205-206206- if (pm)207207- return pm_generic_poweroff(dev);208208- else209209- return spi_legacy_suspend(dev, PMSG_HIBERNATE);210210-}211211-212212-static int spi_pm_restore(struct device *dev)213213-{214214- const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;215215-216216- if (pm)217217- return pm_generic_restore(dev);218218- else219219- return spi_legacy_resume(dev);220220-}221221-#else222222-#define spi_pm_suspend NULL223223-#define spi_pm_resume NULL224224-#define spi_pm_freeze NULL225225-#define spi_pm_thaw NULL226226-#define spi_pm_poweroff NULL227227-#define spi_pm_restore NULL228228-#endif229229-230230-static const struct dev_pm_ops spi_pm = {231231- .suspend = spi_pm_suspend,232232- .resume = spi_pm_resume,233233- .freeze = spi_pm_freeze,234234- .thaw = spi_pm_thaw,235235- .poweroff = spi_pm_poweroff,236236- .restore = spi_pm_restore,237237- SET_RUNTIME_PM_OPS(238238- pm_generic_runtime_suspend,239239- pm_generic_runtime_resume,240240- NULL241241- )242242-};243243-244131struct bus_type spi_bus_type = {245132 .name = "spi",246133 .dev_groups = spi_dev_groups,247134 .match = spi_match_device,248135 .uevent = spi_uevent,249249- .pm = &spi_pm,250136};251137EXPORT_SYMBOL_GPL(spi_bus_type);252138
-4
include/linux/spi/spi.h
···162162 * @remove: Unbinds this driver from the spi device163163 * @shutdown: Standard shutdown callback used during system state164164 * transitions such as powerdown/halt and kexec165165- * @suspend: Standard suspend callback used during system state transitions166166- * @resume: Standard resume callback used during system state transitions167165 * @driver: SPI device drivers should initialize the name and owner168166 * field of this structure.169167 *···182184 int (*probe)(struct spi_device *spi);183185 int (*remove)(struct spi_device *spi);184186 void (*shutdown)(struct spi_device *spi);185185- int (*suspend)(struct spi_device *spi, pm_message_t mesg);186186- int (*resume)(struct spi_device *spi);187187 struct device_driver driver;188188};189189