19787f5e2SThor Thayer /* 20a589167SPaul Gortmaker * Altera Arria10 DevKit System Resource MFD Driver 30a589167SPaul Gortmaker * 40a589167SPaul Gortmaker * Author: Thor Thayer <tthayer@opensource.altera.com> 50a589167SPaul Gortmaker * 69787f5e2SThor Thayer * Copyright Intel Corporation (C) 2014-2016. All Rights Reserved 79787f5e2SThor Thayer * 89787f5e2SThor Thayer * This program is free software; you can redistribute it and/or modify it 99787f5e2SThor Thayer * under the terms and conditions of the GNU General Public License, 109787f5e2SThor Thayer * version 2, as published by the Free Software Foundation. 119787f5e2SThor Thayer * 129787f5e2SThor Thayer * This program is distributed in the hope it will be useful, but WITHOUT 139787f5e2SThor Thayer * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 149787f5e2SThor Thayer * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 159787f5e2SThor Thayer * more details. 169787f5e2SThor Thayer * 179787f5e2SThor Thayer * You should have received a copy of the GNU General Public License along with 189787f5e2SThor Thayer * this program. If not, see <http://www.gnu.org/licenses/>. 199787f5e2SThor Thayer * 209787f5e2SThor Thayer * SPI access for Altera Arria10 MAX5 System Resource Chip 219787f5e2SThor Thayer * 229787f5e2SThor Thayer * Adapted from DA9052 239787f5e2SThor Thayer */ 249787f5e2SThor Thayer 259787f5e2SThor Thayer #include <linux/mfd/altera-a10sr.h> 269787f5e2SThor Thayer #include <linux/mfd/core.h> 270a589167SPaul Gortmaker #include <linux/init.h> 289787f5e2SThor Thayer #include <linux/of.h> 299787f5e2SThor Thayer #include <linux/spi/spi.h> 309787f5e2SThor Thayer 319787f5e2SThor Thayer static const struct mfd_cell altr_a10sr_subdev_info[] = { 329787f5e2SThor Thayer { 339787f5e2SThor Thayer .name = "altr_a10sr_gpio", 349787f5e2SThor Thayer .of_compatible = "altr,a10sr-gpio", 359787f5e2SThor Thayer }, 36*dd47e972SThor Thayer { 37*dd47e972SThor Thayer .name = "altr_a10sr_reset", 38*dd47e972SThor Thayer .of_compatible = "altr,a10sr-reset", 39*dd47e972SThor Thayer }, 409787f5e2SThor Thayer }; 419787f5e2SThor Thayer 429787f5e2SThor Thayer static bool altr_a10sr_reg_readable(struct device *dev, unsigned int reg) 439787f5e2SThor Thayer { 449787f5e2SThor Thayer switch (reg) { 459787f5e2SThor Thayer case ALTR_A10SR_VERSION_READ: 469787f5e2SThor Thayer case ALTR_A10SR_LED_REG: 479787f5e2SThor Thayer case ALTR_A10SR_PBDSW_REG: 489787f5e2SThor Thayer case ALTR_A10SR_PBDSW_IRQ_REG: 499787f5e2SThor Thayer case ALTR_A10SR_PWR_GOOD1_REG: 509787f5e2SThor Thayer case ALTR_A10SR_PWR_GOOD2_REG: 519787f5e2SThor Thayer case ALTR_A10SR_PWR_GOOD3_REG: 529787f5e2SThor Thayer case ALTR_A10SR_FMCAB_REG: 539787f5e2SThor Thayer case ALTR_A10SR_HPS_RST_REG: 549787f5e2SThor Thayer case ALTR_A10SR_USB_QSPI_REG: 559787f5e2SThor Thayer case ALTR_A10SR_SFPA_REG: 569787f5e2SThor Thayer case ALTR_A10SR_SFPB_REG: 579787f5e2SThor Thayer case ALTR_A10SR_I2C_M_REG: 589787f5e2SThor Thayer case ALTR_A10SR_WARM_RST_REG: 599787f5e2SThor Thayer case ALTR_A10SR_WR_KEY_REG: 609787f5e2SThor Thayer case ALTR_A10SR_PMBUS_REG: 619787f5e2SThor Thayer return true; 629787f5e2SThor Thayer default: 639787f5e2SThor Thayer return false; 649787f5e2SThor Thayer } 659787f5e2SThor Thayer } 669787f5e2SThor Thayer 679787f5e2SThor Thayer static bool altr_a10sr_reg_writeable(struct device *dev, unsigned int reg) 689787f5e2SThor Thayer { 699787f5e2SThor Thayer switch (reg) { 709787f5e2SThor Thayer case ALTR_A10SR_LED_REG: 719787f5e2SThor Thayer case ALTR_A10SR_PBDSW_IRQ_REG: 729787f5e2SThor Thayer case ALTR_A10SR_FMCAB_REG: 739787f5e2SThor Thayer case ALTR_A10SR_HPS_RST_REG: 749787f5e2SThor Thayer case ALTR_A10SR_USB_QSPI_REG: 759787f5e2SThor Thayer case ALTR_A10SR_SFPA_REG: 769787f5e2SThor Thayer case ALTR_A10SR_SFPB_REG: 779787f5e2SThor Thayer case ALTR_A10SR_WARM_RST_REG: 789787f5e2SThor Thayer case ALTR_A10SR_WR_KEY_REG: 799787f5e2SThor Thayer case ALTR_A10SR_PMBUS_REG: 809787f5e2SThor Thayer return true; 819787f5e2SThor Thayer default: 829787f5e2SThor Thayer return false; 839787f5e2SThor Thayer } 849787f5e2SThor Thayer } 859787f5e2SThor Thayer 869787f5e2SThor Thayer static bool altr_a10sr_reg_volatile(struct device *dev, unsigned int reg) 879787f5e2SThor Thayer { 889787f5e2SThor Thayer switch (reg) { 899787f5e2SThor Thayer case ALTR_A10SR_PBDSW_REG: 909787f5e2SThor Thayer case ALTR_A10SR_PBDSW_IRQ_REG: 919787f5e2SThor Thayer case ALTR_A10SR_PWR_GOOD1_REG: 929787f5e2SThor Thayer case ALTR_A10SR_PWR_GOOD2_REG: 939787f5e2SThor Thayer case ALTR_A10SR_PWR_GOOD3_REG: 949787f5e2SThor Thayer case ALTR_A10SR_HPS_RST_REG: 959787f5e2SThor Thayer case ALTR_A10SR_I2C_M_REG: 969787f5e2SThor Thayer case ALTR_A10SR_WARM_RST_REG: 979787f5e2SThor Thayer case ALTR_A10SR_WR_KEY_REG: 989787f5e2SThor Thayer case ALTR_A10SR_PMBUS_REG: 999787f5e2SThor Thayer return true; 1009787f5e2SThor Thayer default: 1019787f5e2SThor Thayer return false; 1029787f5e2SThor Thayer } 1039787f5e2SThor Thayer } 1049787f5e2SThor Thayer 1057f70495aSAxel Lin static const struct regmap_config altr_a10sr_regmap_config = { 1069787f5e2SThor Thayer .reg_bits = 8, 1079787f5e2SThor Thayer .val_bits = 8, 1089787f5e2SThor Thayer 1099787f5e2SThor Thayer .cache_type = REGCACHE_NONE, 1109787f5e2SThor Thayer 1119787f5e2SThor Thayer .use_single_rw = true, 1129787f5e2SThor Thayer .read_flag_mask = 1, 1139787f5e2SThor Thayer .write_flag_mask = 0, 1149787f5e2SThor Thayer 1159787f5e2SThor Thayer .max_register = ALTR_A10SR_WR_KEY_REG, 1169787f5e2SThor Thayer .readable_reg = altr_a10sr_reg_readable, 1179787f5e2SThor Thayer .writeable_reg = altr_a10sr_reg_writeable, 1189787f5e2SThor Thayer .volatile_reg = altr_a10sr_reg_volatile, 1199787f5e2SThor Thayer 1209787f5e2SThor Thayer }; 1219787f5e2SThor Thayer 1229787f5e2SThor Thayer static int altr_a10sr_spi_probe(struct spi_device *spi) 1239787f5e2SThor Thayer { 1249787f5e2SThor Thayer int ret; 1259787f5e2SThor Thayer struct altr_a10sr *a10sr; 1269787f5e2SThor Thayer 1279787f5e2SThor Thayer a10sr = devm_kzalloc(&spi->dev, sizeof(*a10sr), 1289787f5e2SThor Thayer GFP_KERNEL); 1299787f5e2SThor Thayer if (!a10sr) 1309787f5e2SThor Thayer return -ENOMEM; 1319787f5e2SThor Thayer 1329787f5e2SThor Thayer spi->mode = SPI_MODE_3; 1339787f5e2SThor Thayer spi->bits_per_word = 8; 1349787f5e2SThor Thayer spi_setup(spi); 1359787f5e2SThor Thayer 1369787f5e2SThor Thayer a10sr->dev = &spi->dev; 1379787f5e2SThor Thayer 1389787f5e2SThor Thayer spi_set_drvdata(spi, a10sr); 1399787f5e2SThor Thayer 1409787f5e2SThor Thayer a10sr->regmap = devm_regmap_init_spi(spi, &altr_a10sr_regmap_config); 1419787f5e2SThor Thayer if (IS_ERR(a10sr->regmap)) { 1429787f5e2SThor Thayer ret = PTR_ERR(a10sr->regmap); 1439787f5e2SThor Thayer dev_err(&spi->dev, "Failed to allocate register map: %d\n", 1449787f5e2SThor Thayer ret); 1459787f5e2SThor Thayer return ret; 1469787f5e2SThor Thayer } 1479787f5e2SThor Thayer 1489787f5e2SThor Thayer ret = devm_mfd_add_devices(a10sr->dev, PLATFORM_DEVID_AUTO, 1499787f5e2SThor Thayer altr_a10sr_subdev_info, 1509787f5e2SThor Thayer ARRAY_SIZE(altr_a10sr_subdev_info), 1519787f5e2SThor Thayer NULL, 0, NULL); 1529787f5e2SThor Thayer if (ret) 1539787f5e2SThor Thayer dev_err(a10sr->dev, "Failed to register sub-devices: %d\n", 1549787f5e2SThor Thayer ret); 1559787f5e2SThor Thayer 1569787f5e2SThor Thayer return ret; 1579787f5e2SThor Thayer } 1589787f5e2SThor Thayer 1599787f5e2SThor Thayer static const struct of_device_id altr_a10sr_spi_of_match[] = { 1609787f5e2SThor Thayer { .compatible = "altr,a10sr" }, 1619787f5e2SThor Thayer { }, 1629787f5e2SThor Thayer }; 1639787f5e2SThor Thayer 1649787f5e2SThor Thayer static struct spi_driver altr_a10sr_spi_driver = { 1659787f5e2SThor Thayer .probe = altr_a10sr_spi_probe, 1669787f5e2SThor Thayer .driver = { 1679787f5e2SThor Thayer .name = "altr_a10sr", 1689787f5e2SThor Thayer .of_match_table = of_match_ptr(altr_a10sr_spi_of_match), 1699787f5e2SThor Thayer }, 1709787f5e2SThor Thayer }; 1710a589167SPaul Gortmaker builtin_driver(altr_a10sr_spi_driver, spi_register_driver) 172