xref: /linux/arch/powerpc/platforms/powernv/opal-sensor.c (revision 6bc08d03e745a0b369bd28294b969e4fdaf7be9f)
17224adbbSNeelesh Gupta /*
27224adbbSNeelesh Gupta  * PowerNV sensor code
37224adbbSNeelesh Gupta  *
47224adbbSNeelesh Gupta  * Copyright (C) 2013 IBM
57224adbbSNeelesh Gupta  *
67224adbbSNeelesh Gupta  * This program is free software; you can redistribute it and/or modify
77224adbbSNeelesh Gupta  * it under the terms of the GNU General Public License as published by
87224adbbSNeelesh Gupta  * the Free Software Foundation; either version 2 of the License, or
97224adbbSNeelesh Gupta  * (at your option) any later version.
107224adbbSNeelesh Gupta  *
117224adbbSNeelesh Gupta  * This program is distributed in the hope that it will be useful,
127224adbbSNeelesh Gupta  * but WITHOUT ANY WARRANTY; without even the implied warranty of
137224adbbSNeelesh Gupta  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147224adbbSNeelesh Gupta  * GNU General Public License for more details.
157224adbbSNeelesh Gupta  *
167224adbbSNeelesh Gupta  * You should have received a copy of the GNU General Public License
177224adbbSNeelesh Gupta  * along with this program; if not, write to the Free Software
187224adbbSNeelesh Gupta  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
197224adbbSNeelesh Gupta  */
207224adbbSNeelesh Gupta 
217224adbbSNeelesh Gupta #include <linux/delay.h>
227224adbbSNeelesh Gupta #include <linux/mutex.h>
238de303baSNeelesh Gupta #include <linux/of_platform.h>
247224adbbSNeelesh Gupta #include <asm/opal.h>
258de303baSNeelesh Gupta #include <asm/machdep.h>
267224adbbSNeelesh Gupta 
277224adbbSNeelesh Gupta static DEFINE_MUTEX(opal_sensor_mutex);
287224adbbSNeelesh Gupta 
297224adbbSNeelesh Gupta /*
307224adbbSNeelesh Gupta  * This will return sensor information to driver based on the requested sensor
317224adbbSNeelesh Gupta  * handle. A handle is an opaque id for the powernv, read by the driver from the
327224adbbSNeelesh Gupta  * device tree..
337224adbbSNeelesh Gupta  */
347224adbbSNeelesh Gupta int opal_get_sensor_data(u32 sensor_hndl, u32 *sensor_data)
357224adbbSNeelesh Gupta {
367224adbbSNeelesh Gupta 	int ret, token;
377224adbbSNeelesh Gupta 	struct opal_msg msg;
389000c17dSAnton Blanchard 	__be32 data;
397224adbbSNeelesh Gupta 
407224adbbSNeelesh Gupta 	token = opal_async_get_token_interruptible();
417224adbbSNeelesh Gupta 	if (token < 0) {
427224adbbSNeelesh Gupta 		pr_err("%s: Couldn't get the token, returning\n", __func__);
437224adbbSNeelesh Gupta 		ret = token;
447224adbbSNeelesh Gupta 		goto out;
457224adbbSNeelesh Gupta 	}
467224adbbSNeelesh Gupta 
477224adbbSNeelesh Gupta 	mutex_lock(&opal_sensor_mutex);
489000c17dSAnton Blanchard 	ret = opal_sensor_read(sensor_hndl, token, &data);
49*6bc08d03SCédric Le Goater 	switch (ret) {
50*6bc08d03SCédric Le Goater 	case OPAL_ASYNC_COMPLETION:
517224adbbSNeelesh Gupta 		ret = opal_async_wait_response(token, &msg);
527224adbbSNeelesh Gupta 		if (ret) {
537224adbbSNeelesh Gupta 			pr_err("%s: Failed to wait for the async response, %d\n",
547224adbbSNeelesh Gupta 			       __func__, ret);
557224adbbSNeelesh Gupta 			goto out_token;
567224adbbSNeelesh Gupta 		}
577224adbbSNeelesh Gupta 
58e3c5c2e0SCédric Le Goater 		ret = opal_error_code(be64_to_cpu(msg.params[1]));
59*6bc08d03SCédric Le Goater 		*sensor_data = be32_to_cpu(data);
60*6bc08d03SCédric Le Goater 		break;
61*6bc08d03SCédric Le Goater 
62*6bc08d03SCédric Le Goater 	case OPAL_SUCCESS:
63*6bc08d03SCédric Le Goater 		ret = 0;
64*6bc08d03SCédric Le Goater 		*sensor_data = be32_to_cpu(data);
65*6bc08d03SCédric Le Goater 		break;
66*6bc08d03SCédric Le Goater 
67*6bc08d03SCédric Le Goater 	default:
68*6bc08d03SCédric Le Goater 		ret = opal_error_code(ret);
69*6bc08d03SCédric Le Goater 		break;
70*6bc08d03SCédric Le Goater 	}
717224adbbSNeelesh Gupta 
727224adbbSNeelesh Gupta out_token:
737224adbbSNeelesh Gupta 	mutex_unlock(&opal_sensor_mutex);
747224adbbSNeelesh Gupta 	opal_async_release_token(token);
757224adbbSNeelesh Gupta out:
767224adbbSNeelesh Gupta 	return ret;
777224adbbSNeelesh Gupta }
787224adbbSNeelesh Gupta EXPORT_SYMBOL_GPL(opal_get_sensor_data);
798de303baSNeelesh Gupta 
808de303baSNeelesh Gupta static __init int opal_sensor_init(void)
818de303baSNeelesh Gupta {
828de303baSNeelesh Gupta 	struct platform_device *pdev;
838de303baSNeelesh Gupta 	struct device_node *sensor;
848de303baSNeelesh Gupta 
858de303baSNeelesh Gupta 	sensor = of_find_node_by_path("/ibm,opal/sensors");
868de303baSNeelesh Gupta 	if (!sensor) {
878de303baSNeelesh Gupta 		pr_err("Opal node 'sensors' not found\n");
888de303baSNeelesh Gupta 		return -ENODEV;
898de303baSNeelesh Gupta 	}
908de303baSNeelesh Gupta 
918de303baSNeelesh Gupta 	pdev = of_platform_device_create(sensor, "opal-sensor", NULL);
928de303baSNeelesh Gupta 	of_node_put(sensor);
938de303baSNeelesh Gupta 
948de303baSNeelesh Gupta 	return PTR_ERR_OR_ZERO(pdev);
958de303baSNeelesh Gupta }
968de303baSNeelesh Gupta machine_subsys_initcall(powernv, opal_sensor_init);
97